If you are looking to do a recursive array search in PHP (and don’t feel like writing your own), you might have tried a number of the functions described in the comments section of the array_search page in the manual. Just for future reference, I have tried a few, but not all of them worked for me. This one did:
/** * Searches haystack for needle and * returns an array of the key path if * it is found in the (multidimensional) * array, FALSE otherwise. * * @mixed array_searchRecursive ( mixed needle, * array haystack [, bool strict[, array path]] ) */ function array_searchRecursive( $needle, $haystack, $strict=false, $path=array() ) { if( !is_array($haystack) ) { return false; } foreach( $haystack as $key => $val ) { if( is_array($val) && $subPath = array_searchRecursive($needle, $val, $strict, $path) ) { $path = array_merge($path, array($key), $subPath); return $path; } elseif( (!$strict && $val == $needle) || ($strict && $val === $needle) ) { $path[] = $key; return $path; } } return false; } |
The comment directly following it (above it, actually) describes a modified version that would let you restrict your search by key.
18 Comments
Well this does exactly what I need. Saved me a lot of time, thanks!
Thanks, works great! You might want to replace the > and &’s to normal characters.
Was exactly what I needed, thanks!
This is a copy of the php.net manual user contributions.
However it does not perform correctly.
What if an occurrence of the needle is found in the array more than once ?
Thank you! I also couldn’t get the examples on php.net to work in my script. This worked almost immediately!
well this is not what i was searching for , but it surely helped me think of the logic to make my own function for a certain process :) Thanks a lot .
I get the following error:
Parse error: syntax error, unexpected ‘=’, expecting ‘)’ in myphppage.php on line 132
Line 132 being : foreach( $haystack as $key => $val ) {
Any ideas ?
Be sure your multidimensional array contain boolean true false values. if it contains call the function with $strict=true,function is awesome and very helpful.
help me.if used recursion:
bool contains( char *haystack, char *needle)
How would I modify this to work with this array: http://i56.tinypic.com/345hdex.png
Great… exactly what i wanted… thanx..
I’m back with a solution.
Using these 2 functions will search multidimensional arrays and return ALL matching paths (items) NOT just a single item.
if(!function_exists(‘array_search_recursive’)){
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;
}
}
// Recursive backtracking function for multidimensional array search
if(!function_exists(‘search_r’)){
function search_r($value, $array){
$results = array();
if(is_array($array)){
$path = array_search_recursive($value, $array);
if (is_array($path) && count($path) > 0){
$results[] = $path;
unset($array[$path[0]]);
$results = array_merge($results, search_r($value, $array));
}else{
}
}
return $results;
}
}
Works great! Make sure to replace > with a greater than symbol and the $amp; with the Ampersand (6x). Thanks for providing this.
thank you very much!
saved enough time to search value in a multi dimentional array
great ! helped me to solve my pb thx :)
Thank you
here is a easy search function
function search($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, search($subarray, $key, $value));
}
return $results;
}
function recursiveSearch($some_arr){
$found = array(); //create a new array
foreach($some_arr as $key => $val) {
//echo “KEY::”.$key.”VALue::”.$val.”";
if($val == ’1′){//replace ’1′ with the value you want to search
$found[] = $key;
}
}
return $found;
}
Post a Comment