How to check if a value is in a multidimensional array

How to check if a value is in a multidimensional array

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to check if a value is in a multidimensional array

When working with flat arrays, in_array() is a straightforward tool for existence checks. The syntax is predictable: you pass the needle and the haystack, and PHP returns a boolean. However, things get tricky when your data structure nests values deeper than one level. Consider this scenario where you have grouped operating systems into categories:

$b = array(
    array("Mac", "NT"), 
    array("Irix", "Linux")
);

If you try to verify whether "Irix" exists in $b using the standard in_array() function, it will return false. This happens because in_array() performs a linear search only on the top-level keys. It checks the first element (which is an array), sees that it doesn't match the string "Irix", and immediately stops. It never looks inside the sub-arrays to find your target value.

You might assume you simply need to iterate manually, but before reaching for a foreach loop or writing custom recursion logic, it is worth checking if PHP provides a native solution that handles this depth automatically. The answer lies in the optional third parameter of in_array().

Starting from PHP 5.3, in_array() accepts a boolean flag to enable recursive searching. By setting this flag to true, you instruct the function to descend into nested arrays if the initial match fails. Here is how you apply it to the multidimensional structure:

if (in_array("Irix", $b, true)) {
    echo "Got Irix";
}

This approach leverages the built-in engine to handle the traversal logic efficiently. It returns true because the function finds "Irix" inside the second sub-array. If you were checking for a value that exists only in the first sub-array, like "Mac", this method would also succeed.

There is an important consideration regarding case sensitivity and flags. By default, in_array() performs strict type checking and is sensitive to case differences unless you specify loose comparison modes. In multidimensional contexts, if your data contains mixed types (strings mixed with integers), the recursive flag ensures it checks deeper levels regardless of the top-level types, but the strictness of the value comparison remains governed by the second parameter (which defaults to true).

However, relying solely on recursion isn't always the most performant or maintainable choice for massive datasets. If you are dealing with very large arrays where performance is critical, a recursive function call adds overhead compared to a simple linear scan. Furthermore, if your multidimensional array structure is inconsistent—say, some levels contain objects or associative arrays with specific keys—you might need more granular control than in_array() offers. In those cases, a custom recursive function or leveraging Laravel's collection helpers becomes preferable.

If you are working within the Laravel ecosystem, the contains method on collections handles multidimensional checks elegantly without needing to write raw recursion logic. You can pass a nested array as the key and value to search for, allowing you to verify existence across any depth of association. For instance:

$collection = collect($b);
if ($collection->contains('Irix')) {
    // Value found anywhere in the structure
}

This abstraction removes the need to worry about the specific nesting level or to manually manage the recursion stack yourself. It encapsulates the complexity of traversing nested structures while keeping your code readable and expressive.

Ultimately, the decision depends on your PHP version and whether you are using a framework. If you are on an older version of PHP, in_array() lacks the recursive flag entirely, forcing you to write the traversal logic manually or convert arrays to collections if available. In modern environments, adding the third parameter is usually the cleanest, most idiomatic solution that avoids reinventing the wheel while maintaining clarity and correctness.

Our network