note 69232 added to function.array-search
| From: | ob at babcom dot biz | Date: | Mon, 28 Aug 2006 09:55:50 +0000 |
| Subject: | note 69232 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116420@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 66.163.161.117
Probable Submitter: 84.185.247.44
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/69232
Del: integrated -- https://master.php.net/note/delete/69232/integrated
Del: useless -- https://master.php.net/note/delete/69232/useless
Del: bad code -- https://master.php.net/note/delete/69232/bad+code
Del: spam -- https://master.php.net/note/delete/69232/spam
Del: non-english -- https://master.php.net/note/delete/69232/non-english
Del: in docs -- https://master.php.net/note/delete/69232/in+docs
Del: other reasons-- https://master.php.net/note/delete/69232
Reject -- https://master.php.net/note/reject/69232
Search -- https://master.php.net/manage/user-notes.php