stack stack - 7 months ago 9
PHP Question

How can I search in nested arrays?

I have a variable which is containing these nested arrays:

echo $var;

/* Output:
Array(
[0] => Array
(
[id] => 1
[box] => 0
)

[2] => Array
(
[id] => 2
[box] => 0
)

[3] => Array
(
[id] => 3
[box] => 1
)
) */


Now I would like to know, is there any
[box] => 1
in
$var
? How can I do that? Actually I can search in array by
in_array()
function, but I don't know how can I do that for nested arrays?

Answer

You could use array_column:

$index = array_search(1, array_column($var, 'box'));
Comments