34
arraysearchassoc
array_search_assoc
SYNOPSIS
array_search_assoc($array, $key, $value, $path=null)
DESCRIPTION
array_search_assoc searches the multidimensional array $array for the first occurrence of the key $key associated with the value $value.
If a match is found, array_search_assoc returns the sequence of keys designating the location of the value.
If no match is found, array_search_assoc returns false.
EXAMPLE
To search for a value in a multidimensional array:
php > require 'library/dump.php';
php > require 'library/arraysearchassoc.php';
php > $arr=array('user' => array('name' => 'izend', 'mail' => 'webmaster@izend.org'));
php > dump(array_search_assoc($arr, 'mail', 'webmaster@izend.org'));
array(2) {
[0] => string(4) "user"
[1] => string(4) "mail"
}
CODE
- function array_search_assoc($array, $key, $value, $path=null) {
- if (array_key_exists($key, $array) && $array[$key] == $value) {
- $path[]=$key;
- return $path;
- }
- foreach ($array as $k => $v ) {
- if (is_array($v)) {
- $path[]=$k;
- $p = array_search_assoc($v, $key, $value, $path);
- if ($p !== false) {
- return $p;
- }
- }
- }
- return false;
- }
Comments