note 92991 deleted from function.array-search by levim
| From: | levim@php.net | Date: | Mon, 24 Feb 2014 22:43:55 +0000 |
| Subject: | note 92991 deleted from function.array-search by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198696@lists.php.net to get a copy of this message | ||
Note Submitter: Frank A Cefalu frankcefalu at gmail dot com
----
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;
}
?>