As you edited your question and there are more values inside each element to test for, array_intersect_assoc
came to mind. It takes care of the cases where each element must not contain the key you're looking for.
Basically: If the intersection between an element and the needle is the needle, an element matches.
This comparison needs to be applied per each element, e.g. with a foreach
. Wrapped into a function that function can return the key and FALSE
if not found:
function array_search_array(array $haystack, array $needle)
{
foreach($haystack as $key => $element)
{
if ($needle == array_intersect_assoc($element, $needle))
return $key;
}
return FALSE;
}
The naming of the function is not optimal I must admit. Demo
This works as well if you need to search for multiple values (regardless of the order of those in $needle
).
As long as you only need to search for a single key/value pair, the key should be checked prior accessing it. This is a modification of the code-example in the answer by Tom:
$foundAt = null;
foreach($yourArray as $index => $pair)
{
if(isset($pair['uid']) && $pair['uid'] === 10)
{
$foundAt = $index;
break;
}
}
echo $foundAt;