note 41969 deleted from function.array-search by felipe
| From: | felipe@php.net | Date: | Sun, 10 Feb 2008 13:10:44 +0000 |
| Subject: | note 41969 deleted from function.array-search by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-135960@lists.php.net to get a copy of this message | ||
Note Submitter: manicdepressive at mindless dot com
----
<?php
/**
recursively descend an arbitrarily deep multidimensional
array, stopping at the first occurence of scalar $needle.
return the path to $needle as an array (list) of keys
if not found, return null.
(will infinitely recurse on self-referential structures)
*/
function array_search_recursive( $needle, $haystack )
{
$path = NULL;
$keys = array_keys($haystack);
while (!$path && (list($toss,$k)=each($keys))) {
$v = $haystack[$k];
if (is_scalar($v)) {
if ($v===$needle) {
$path = array($k);
}
} elseif (is_array($v)) {
if ($path=array_search_recursive( $needle, $v )) {
array_unshift($path,$k);
}
}
}
return $path;
}
?>
code till dawn! -mark meves