How do I search for 2 values in the same record using in_array()? For example I want to search for 'AuthorNiceName' value and 'AuthorType' value.
The below code does not work how I would like. It should return "EU TEST" but it returns "CP TEST".
Thanks in advance.
$authors = array(
array(
'AuthorName' => 'John Smith',
'AuthorNiceName' => 'john-smith',
'AuthorType' => 'CP'
),
array(
'AuthorName' => 'Joe Bloggs',
'AuthorNiceName' => 'joe-bloggs',
'AuthorType' => 'EU'
),
);
if (in_array('joe-bloggs', array_column($authors, 'AuthorNiceName')) && in_array('EU', array_column($authors, 'AuthorType'))) {
$authorType = 'CP TEST';
}
else {
$authorType = 'EU TEST';
}
echo $authorType;
UPDATE: My latest code using @Philipp's suggestion which I adjusted slightly. However it doesn't work, if there are other users with the same 'AuthorType' it returns "no match"?
$authors = array( //TODO
array(
'AuthorName' => 'John Smith',
'AuthorNiceName' => 'john-smith',
'AuthorType' => 'CP'
),
array(
'AuthorName' => 'Joe Bloggs',
'AuthorNiceName' => 'joe-bloggs',
'AuthorType' => 'EU'
),
array(
'AuthorName' => 'Matt Bat',
'AuthorNiceName' => 'matt-bat',
'AuthorType' => 'EU'
),
);
$name = 'joe-bloggs';
$type = 'EU';
foreach ($authors as $author) {
if ($author['AuthorNiceName'] == $name && $author['AuthorType'] == 'EU') {
$authorType = 'EU Test';
}
elseif ($author['AuthorNiceName'] == $name && $author['AuthorType'] == 'CP') {
$authorType = 'CP Test';
}
else {
$authorType = 'no match';
}
}
echo $authorType; //returns "not match". it should return "EU Test".