note 88710 added to function.array-search
| From: | info at [hidden] dot apanel dot nl | Date: | Wed, 04 Feb 2009 13:27:33 +0000 |
| Subject: | note 88710 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150201@lists.php.net to get a copy of this message | ||
I made this function after I didnt find a recursive array_search function with a limit and the
possibility to inverse the search. So here is it:
<?php
function search_in_array ($needle, $haystack, $inverse = false, $limit = 1) {
# Settings
$path = array ();
$count = 0;
# Check if inverse
if ($inverse == true)
$haystack = array_reverse ($haystack, true);
# Loop
foreach ($haystack as $key => $value) {
# Check for return
if ($count > 0 && $count == $limit)
return $path;
# Check for val
if ($value === $needle) {
# Add to path
$path[] = $key;
# Count
$count++;
} else if (is_array ($value)) {
# Fetch subs
$sub = search_in_array ($needle, $value, $inverse, $limit);
# Check if there are subs
if (count ($sub) > 0) {
# Add to path
$path[$key] = $sub;
# Add to count
$count += count ($sub);
}
}
}
return $path;
}
?>
----
Server IP: 213.249.64.162
Probable Submitter: 195.35.224.66
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/88710
Del: integrated -- https://master.php.net/note/delete/88710/integrated
Del: useless -- https://master.php.net/note/delete/88710/useless
Del: bad code -- https://master.php.net/note/delete/88710/bad+code
Del: spam -- https://master.php.net/note/delete/88710/spam
Del: non-english -- https://master.php.net/note/delete/88710/non-english
Del: in docs -- https://master.php.net/note/delete/88710/in+docs
Del: other reasons-- https://master.php.net/note/delete/88710
Reject -- https://master.php.net/note/reject/88710
Search -- https://master.php.net/manage/user-notes.php