You can't use in_array
outright since it'll only accept a flat array. Just use array_column
to get all state
values, then make your checking.
Here's the idea:
if (in_array($name, array_column($countries, 'state'))) {
}
Basically, if your $countries
are multi leveled, (meaning):
Array
(
[0] => Array
(
[state] => Arizona
[date] => 2018-01-01
)
[1] => Array
(
[state] => California
[date] => 2018-01-01
)
)
When you apply array_column
, you sorta pluck out each state
values, then gather them inside an array. It will yield an array like this:
array_column($countries, 'state') ===> Array ( [0] => Arizona [1] => California )
Only then you can use in_array
. Just check out the manual, its eloquently stated there very well.