How to search Array for multiple values in PHP?

How to search Array for multiple values in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

Finding All Keys for Duplicate Values in PHP Arrays

When you need to locate every index that holds a specific value in an array, array_search is often the first tool that comes to mind. It works perfectly for finding a single match, but it returns only the first occurrence. If you are building a form validator or processing transaction logs where duplicate dates or statuses matter, relying on this default behavior will leave you with incomplete data. You need a way to return all keys for duplicate values, which array_search does not provide out of the box.

In your specific case, you have a nested array structure where $list[0] contains date strings, and you want to find all indices that hold the value "2009-09-09". The output should be an array containing both 0 and 2. While you could technically write a loop to check every element, PHP provides built-in functions that handle this logic more cleanly.

The most direct approach for finding all keys is using array_keys. Unlike array_search, which stops after the first match, array_keys returns an array of all keys where the value matches your search criteria. You pass the target value as the first argument and the haystack (the array to search within) as the second.

$searchValue = "2009-09-09";
$keys = array_keys($list[0], $searchValue);
print_r($keys); 
// Output: Array ( [0] => 0 [1] => 2 )

This solution is concise and leverages the engine's internal iteration. It assumes that your array keys are numeric, which is standard for sequential lists like yours. If your array contains string keys or is sparse, array_keys will still return the actual keys associated with the matching values, ensuring your logic remains robust regardless of how the data was populated.

For nested arrays like your $list structure where you need to inspect specific columns (e.g., column index 2), you must isolate the relevant sub-array before searching. Wrapping the array_keys call inside a conditional check or iterating over the outer array allows you to collect duplicates across multiple rows. For example, if you wanted to find every row that contains the date "2009-09-09" in the third column:

$duplicateRows = [];
foreach ($list as $index => $row) {
    if (in_array($searchValue, $row)) {
        // Store the index of the row and potentially the specific key found
        $duplicateRows[] = ['row_index' => $index];
    }
}

This pattern is preferable when dealing with multi-dimensional data because it gives you context about which "record" or "row" contained the duplicate. You can further refine this by capturing the exact column index if needed, allowing for precise debugging or reporting.

There are trade-offs to consider depending on your performance requirements. array_keys performs a linear scan, so its time complexity is O(n). For extremely large datasets where performance is critical, you might consider building an associative array (dictionary) of values to keys in a single pass using array_column or a loop, then using array_search with the second argument set to true to find all occurrences. However, for typical application sizes, the overhead of creating an intermediate map often outweighs the benefits of a more complex algorithm. The built-in functions are optimized and readable.

If you are working within the Laravel ecosystem, this logic maps directly to Eloquent queries or collection methods. When processing collections, the whereIn method combined with pluck or looping through filtered results offers similar functionality with better readability for model data. For instance, you might filter a collection by a specific attribute value and then extract the keys of all matching items.

Ultimately, avoiding manual loops in favor of standard PHP functions keeps your code maintainable and less prone to off-by-one errors. Whether you are validating input dates or filtering transaction statuses, understanding how array_keys differs from array_search ensures you capture every instance of a duplicate value without writing unnecessary custom logic.

Our network