1

I have following arrays :

<?php 
$a=array(
    "abc"=>array("red","white","orange"),
    "def"=>array("green","vilot","yellow"),
    "xyz"=>array("blue","dark","pure")
); 

echo array_search(array("dark"),$a);

?>

How to get output of xyz in array list.

1

3 Answers 3

1

array_search returns false or the key. Since you have multiple dimensions you must loop through to get the lowest level.

Since we are in another dimension your return will actually be 1. For this reason, if array_search succeeds we must use the key that is defined in the foreach

<?php
$a=array("abc"=>array("red","white","orange"),"def"=>array("green","vilot","yellow"),"xyz"=>array("blue","dark","pure")); 
foreach($a as $key=>$data){
    if(array_search("dark",$data)){
        echo $key;

    }    

}

Outputs: xyz

Sign up to request clarification or add additional context in comments.

Comments

1

You can create one user-define function to check value

 $a=array("abc"=>array("red","white","orange"),"def"=>array("green","vilot","yellow"),"xyz"=>array("blue","dark","pure")); 
 function search_data($value, $array) {
   foreach ($array as $key => $val) {
       if(is_array($val) && in_array($value,$val))
       {
           return $key;
       }
   }
   return null;
}
echo search_data("dark",$a);

DEMO

Comments

0

Please try this

function searchMultiArray($arrayVal,$val){
    foreach($arrayVal as $key => $suba){

        if (in_array($val, $suba)) {
         return $key;
     }

 }
}

$a = array("abc"=>array("red","white","orange"),"def"=>array("green","vilot","yellow"),"xyz"=>array("blue","dark","pure"));
echo $keyVal = searchMultiArray($a , "dark");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.