note 41969 added to function.array-search
| From: | manicdepressive at mindless dot com | Date: | Tue, 27 Apr 2004 23:49:23 +0000 |
| Subject: | note 41969 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-68915@lists.php.net to get a copy of this message | ||
<?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
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41969
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41969&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41969&report=yes
Search -- http://master.php.net/manage/user-notes.php