note 96866 deleted from function.array-search by levim
| From: | levim@php.net | Date: | Mon, 24 Feb 2014 22:45:10 +0000 |
| Subject: | note 96866 deleted from function.array-search by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198657@lists.php.net to get a copy of this message | ||
Note Submitter: kim at steinhaug dot com
----
I was looking around for a recursive search by keys in multidimensional arrays. After testing the
ones in this thread seems noone accually works. So I put this one together, and it does what you
expect it to do.
You need to find the value for the key "han-solo".
<?php
if( ($val = array_search_key('han-solo',$array)) !== false){
var_dump($val);
} else {
// No keys with the name "han-solo"
}
function array_search_key( $needle_key, $array ) {
foreach($array AS $key=>$value){
if($key == $needle_key) return $value;
if(is_array($value)){
if( ($result = array_search_key($needle_key,$value)) !== false)
return $result;
}
}
return false;
}
?>