array_search in multidimensional php code example
Example 1: array search by key in php
$arr = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
function find($mot){
global $arr;
$ok=false;
foreach ($arr as $k => $v)
{
if($k==$mot){
return $v; $ok=true;
}
}
if(ok==false){ return -1; }
}
echo find("two");
Example 2: php search multidimensional array for multiple values
function multi_array_search($array, $search) {
$result = array();
foreach ($array as $key => $value){
foreach ($search as $k => $v){
if (!isset($value[$k]) || $value[$k] != $v){
continue 2;
}
}
$result[] = $key;
}
return $result;
}
print_r(multi_array_search($list_of_phones, array()));
print_r(multi_array_search($list_of_phones, array('Manufacturer' => 'Apple')));
print_r(multi_array_search($list_of_phones, array('Manufacturer' => 'Apple', 'Model' => 'iPhone 6')));