note 77863 added to function.array-search
| From: | logicearth at gmail dot com | Date: | Sun, 16 Sep 2007 22:43:15 +0000 |
| Subject: | note 77863 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130544@lists.php.net to get a copy of this message | ||
There is probably a much more efficient way to do this, handle an unlimited depth but while
supporting single declensions arrays.
<?php
function array_search_recursive ($needle, $haystack, $strict = false, $depth = 0) {
$tmp = array_search($needle, $haystack, $strict);
if ($tmp !== false) {
return array($depth => $tmp);
}
foreach ($haystack as $key => $value) {
if (is_array($value)) {
$tmp = array_search_recursive($needle, $value, $strict, ++$depth);
if ($tmp !== false) {
return array_merge(array($depth =>$key), $tmp);
}
}
}
return false;
}
$a = array(
'image/gif' => 'gif',
'image/jpeg' => array('jpg', 'jpeg', 'jpe',
'more' => array('n', 'm', 'x', 'find me')),
'image/png' => 'png'
);
var_dump(array_search_recursive('find me', $a));
?>
----
Server IP: 69.147.83.197
Probable Submitter: 76.171.101.17
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/77863
Del: integrated -- https://master.php.net/note/delete/77863/integrated
Del: useless -- https://master.php.net/note/delete/77863/useless
Del: bad code -- https://master.php.net/note/delete/77863/bad+code
Del: spam -- https://master.php.net/note/delete/77863/spam
Del: non-english -- https://master.php.net/note/delete/77863/non-english
Del: in docs -- https://master.php.net/note/delete/77863/in+docs
Del: other reasons-- https://master.php.net/note/delete/77863
Reject -- https://master.php.net/note/reject/77863
Search -- https://master.php.net/manage/user-notes.php