note 108783 added to function.array-search
| From: | spotitbiz at gmail dot com | Date: | Thu, 24 May 2012 10:27:54 +0000 |
| Subject: | note 108783 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188190@lists.php.net to get a copy of this message | ||
I have just tried to add the function of recursive Array Search in order to return all finding keys
in an array instead of the first matching result index, and further expanded search to contain
needle instead of exactly matching:
function recursiveArraySearchAll($haystack, $needle, $index = null)
{
$aIt = new RecursiveArrayIterator($haystack);
$it = new RecursiveIteratorIterator($aIt);
$resultkeys;
while($it->valid()) {
if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND
(strpos($it->current(), $needle)!==false)) { //$it->current() == $needle
$resultkeys[]=$aIt->key(); //return $aIt->key();
}
$it->next();
}
return $resultkeys; // return all finding in an array
} ;
To use the function on your array search:
print_r(recursiveArraySearchAll($your-array,'your-needle-to
match','your-array-index-to-search') );
Simple! and Enjoy!
----
Server IP: 69.147.83.197
Probable Submitter: 118.93.149.130
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/108783
Del: integrated -- https://master.php.net/note/delete/108783/integrated
Del: useless -- https://master.php.net/note/delete/108783/useless
Del: bad code -- https://master.php.net/note/delete/108783/bad+code
Del: spam -- https://master.php.net/note/delete/108783/spam
Del: non-english -- https://master.php.net/note/delete/108783/non-english
Del: in docs -- https://master.php.net/note/delete/108783/in+docs
Del: other reasons-- https://master.php.net/note/delete/108783
Reject -- https://master.php.net/note/reject/108783
Search -- https://master.php.net/manage/user-notes.php