note 103644 deleted from function.array-search by danbrown
| From: | danbrown@php.net | Date: | Tue, 26 Apr 2011 14:52:25 +0000 |
| Subject: | note 103644 deleted from function.array-search by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-179164@lists.php.net to get a copy of this message | ||
Note Submitter: faruzzy at gmail dot com
----
This is a function that I wrote which works like the search array function, it searches the array
for a given value and returns the corresponding key if successful. But this one goes one deeper in
the case an array also contains one or many other array.
example:
$letters_map = array(
'2' => array('a', 'b', 'c'),
'3' => array('d', 'e', 'f'),
'4' => array('g', 'h', 'i'),
'5' => array('j', 'k', 'l'),
'6' => array('m', 'n', 'o'),
'7' => array('p', 'q', 'r', 's'),
'8' => array('t', 'u', 'v'),
'9' => array('w', 'x', 'y', 'z')
);
multi_array_search('n', $letters_map); //returns 6
function multi_array_search($needle, $haystackArray){
$Key;
foreach($haystackArray as $key => $value){
if(is_array($value)){
$value = array_search($needle, $value);
if(!empty($value)){//needle is found
$Key = $key;
break;
}
}
}
return $Key;
}