note 87060 deleted from function.array-search by levim
| From: | levim@php.net | Date: | Mon, 24 Feb 2014 22:36:22 +0000 |
| Subject: | note 87060 deleted from function.array-search by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198640@lists.php.net to get a copy of this message | ||
Note Submitter: michi7x7
----
If you exit the function whith "return", there is no need to break ;)
i would write it like this:
<?php
function array_search_recursive($needle, $haystack, $path=array())
{
foreach($haystack as $id => $val)
{
$path2=$path;
$path2[] = $id;
if($val === $needle)
return $path2;
else if(is_array($val))
if($ret = array_search_recursive($needle, $val, $path2))
return $ret;
}
return false;
}
$array = array(
"a" => array(
"b" => array("c"),
"d" => array("e"),
),
"f" => array( "g", "h"),
);
var_dump(array_search_recursive("e", $array));
?>
var dump prints a array: (a, d, 0)