note 69232 deleted from function.array-search by levim
| From: | levim@php.net | Date: | Mon, 24 Feb 2014 22:40:37 +0000 |
| Subject: | note 69232 deleted from function.array-search by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198645@lists.php.net to get a copy of this message | ||
Note Submitter: ob at babcom dot biz
----
This function is based on the function in comment "array_search" from July 26th 2006.
I added the possibility of defining the key which $Needle shall be searched for.
<?php
// search haystack for needle and return an array of the key path,
// FALSE otherwise.
// if NeedleKey is given, return only for this key
// mixed ArraySearchRecursive(mixed Needle,array Haystack
// [,NeedleKey[,bool Strict[,array Path]]])
function ArraySearchRecursive($Needle,$Haystack,$NeedleKey="",
$Strict=false,$Path=array()) {
if(!is_array($Haystack))
return false;
foreach($Haystack as $Key => $Val) {
if(is_array($Val)&&
$SubPath=ArraySearchRecursive($Needle,$Val,$NeedleKey,
$Strict,$Path)) {
$Path=array_merge($Path,Array($Key),$SubPath);
return $Path;
}
elseif((!$Strict&&$Val==$Needle&&
$Key==(strlen($NeedleKey)>0?$NeedleKey:$Key))||
($Strict&&$Val===$Needle&&
$Key==(strlen($NeedleKey)>0?$NeedleKey:$Key))) {
$Path[]=$Key;
return $Path;
}
}
return false;
}
?>
Remove unnecessary new lines. I had to add them because of too long lines.