note 92991 added to function.array-search
| From: | FrankACefalufrankcefalu at gmail dot com | Date: | Tue, 18 Aug 2009 18:22:43 +0000 |
| Subject: | note 92991 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159608@lists.php.net to get a copy of this message | ||
The original function for searching a multidimensional array didn't let you filter by
arraykeys.
This one is modified so you can search by array keys or not.
If you want a general search for a value and don't care which specific key to target you dont
have to specify it but example.
<?php
$array = ("shoes" => array("test1"=>
"123","test2"=>"1234","test3"=>"12345"),
"shoes2"=>
array("test1"=>"324","test2"=>"3515","test3"=>"123131");
array_search_value("123",$array,"test1"); // would return shoes
array_search_value("12223",$array,"test1"); // would return false;
?>
this makes it easier to validate a multi-dimensional array to make sure certain data is present.
Enjoy!
<?php
function array_search_value($needle,$haystack,$arraykey=FALSE) {
foreach($haystack as $key=>$value) {
$current_key=$key;
if($arraykey){
if($needle == $value[$arraykey]){
return $value['id'];
}
if(array_search_value($needle,$value[$arraykey]) == true) {
return $current_key;
}
}else{
if($needle == $value)
return $value;
if(array_search_value($needle,$value) == true) {
return $current_key;
}
}
}
return false;
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 38.105.191.226
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/92991
Del: integrated -- https://master.php.net/note/delete/92991/integrated
Del: useless -- https://master.php.net/note/delete/92991/useless
Del: bad code -- https://master.php.net/note/delete/92991/bad+code
Del: spam -- https://master.php.net/note/delete/92991/spam
Del: non-english -- https://master.php.net/note/delete/92991/non-english
Del: in docs -- https://master.php.net/note/delete/92991/in+docs
Del: other reasons-- https://master.php.net/note/delete/92991
Reject -- https://master.php.net/note/reject/92991
Search -- https://master.php.net/manage/user-notes.php