Skip to content
Advertisement

Determine if specific value exists in any level of a multidimensional array

For this nested array:

JavaScript

I want to know if a certain value “ERROR” exists at least once in the array. I don’t care what key it’s associated with, I just want to know if the $status array contains an “ERROR” status anywhere in it.

Is there a cleaner way to do this than just iterating over the elements with nested for loops?

Advertisement

Answer

You could use the function array_walk_recursive() to get all the values with any level of nested array.

https://secure.php.net/manual/en/function.array-walk-recursive.php

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement