I am having multiple column array and want to test existence of an id key
but meeting the gender criteria column as well.
I am trying to achieve this without looping the array.
<?php
// An array that represents a possible record set returned from a database
$a = array(
array(
'id' => 5698,
'first_name' => 'Peter',
'gender' => 'Male',
),
array(
'id' => 4767,
'first_name' => 'Ann',
'gender' => 'Female',
),
array(
'id' => 3809,
'first_name' => 'Joe',
'gender' => 'Male',
)
);
?>
To test if joe id key exists i am able to
<?php
if (array_key_exists(3809), $a)) {
echo ("That key WAS found");
}else{
echo "That key was NOT found";
}
?>
My difficulty is i need to test array_key_exists(3809), $a where gender equals Male.
Is there a way to achieve this without looping the multi column array? Something like.
if (array_key_exists(3809) and gender == 'Male' , $a)) {
echo ("That key WAS found");
}else{
echo "That key was NOT found";
}
?>
Anyone with idea kindly share.
CodePudding user response:
Just compare the element with the gender
key normally after testing whether the key exists.
if (array_key_exists(3809, $a) && $a[3809]['gender'] == 'Male') {