note 47579 added to function.array-search
| From: | pornsak at neowin dot net | Date: | Sun, 21 Nov 2004 15:28:15 +0000 |
| Subject: | note 47579 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-80835@lists.php.net to get a copy of this message | ||
This is a modified version of Mark Meves's wonderful function. I needed something that would be
able to let me force search the key name where the needle should be found.
<?php
function array_search_recursive($needle, $haystack, $key_lookin="") {
$path = NULL;
if (!empty($key_lookin) && array_key_exists($key_lookin, $haystack) && $needle ===
$haystack[$key_lookin]) {
$path[] = $key_lookin;
} else {
foreach($haystack as $key => $val) {
if (is_scalar($val) && $val === $needle && empty($key_lookin)) {
$path[] = $key;
break;
}
elseif (is_array($val) && $path = array_search_recursive($needle, $val, $key_lookin)) {
array_unshift($path, $key);
break;
}
}
}
return $path;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47579
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47579&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47579&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47579&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47579&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47579&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47579&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47579&report=yes
Search -- http://master.php.net/manage/user-notes.php