You could achieve it like this :
- Reduce your array to a single dimensional array using combination of array_reduce and array_merge PHP functions .
- In the reduced array , look for the key using array_key_exists .
Your array :
$yourArray = array
(
array( 'dog' => true )
,array( 'cat' => true )
,array( 'bird' =>false )
,array( 'fish' => true )
);
Code to check if a key exists :
$itemToFind = 'cat'; // turtle
$result =
array_key_exists
(
$itemToFind
,array_reduce(
$yourArray
,function ( $v , $w ){ return array_merge( $v ,$w ); }
,array()
)
);
var_dump( $result );
Code to check if a key exits and to retrieve its value :
$itemToFind = 'cat'; // bird
$result =
array_key_exists
(
$itemToFind
,$reducedArray = array_reduce(
$yourArray
,function ( $v , $w ){ return array_merge( $v ,$w ); }
,array()
)
) ?$reducedArray[ $itemToFind ] :null;
var_dump( $result );
Using PHP > 5.5.0
You could use combination of array_column and count PHP functions to achieve it :
Code to check if a key exists :
$itemToFind = 'cat'; // turtle
$result = ( count ( array_column( $yourArray ,'cat' ) ) > 0 ) ? true : false;
var_dump( $result );
The above code tested with PHP 5.5.5 is here