How to search through a JSON Array in PHP

How to search through a JSON Array in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

How to search through a JSON Array in PHP

When you receive data from an API or load it from a file, it often arrives as a JSON string. If your application logic requires isolating specific records based on an identifier, you need to parse that string into a usable structure before performing the lookup. The example provided shows a standard nested structure with a people array containing objects with id and content properties.

To retrieve the content associated with ID 8097, you first need to decode the JSON string into a PHP array. Once decoded, you have two primary approaches: iterating through the entire list or leveraging native array functions to find the specific index. The iteration method is robust and works well when you need to handle multiple matches or complex filtering logic later in your code.

$jsonString = '{
  "people":[
    {
      "id": "8080",
      "content": "foo"
    },
    { 
      "id": "8097",
      "content": "bar"
    }
  ]
}';

$data = json_decode($jsonString, true);

// Iterate through the array to find the specific ID
foreach ($data['people'] as $person) {
    if ($person['id'] === '8097') {
        echo $person['content']; // Outputs: bar
        break;
    }
}

This approach is straightforward and readable. It stops processing immediately after finding the first match, which prevents unnecessary memory usage if the dataset is large. However, it requires writing an explicit condition for every field you might search on. If your business logic changes and you need to search by content instead of id, you must modify the conditional statement inside the loop.

For scenarios where performance matters or when working with very large datasets, using array_column combined with array_search can offer a cleaner syntax for single-field lookups. This technique extracts a specific key from every element in the array and creates a temporary index map.

// Extract all IDs into a new array
$ids = array_column($data['people'], 'id');

// Search for the ID and get the corresponding index
$index = array_search('8097', $ids);

if ($index !== false) {
    // Access the content using the found index
    echo $data['people'][$index]['content']; 
}

While this method is concise, it creates an intermediate array of IDs. In a high-throughput environment, maintaining that extra structure in memory adds a slight overhead compared to stopping early during iteration. Furthermore, array_column returns all values even if you only need one; the subsequent array_search scan happens regardless of whether the target exists earlier in the list.

When dealing with real-world data, you must account for edge cases. The JSON string might contain null characters or unescaped quotes that cause json_decode to fail, returning null. Always verify the result of json_decode before accessing nested keys. Additionally, IDs are often stored as strings in JSON but might be treated as integers in your logic. Using loose comparison (==) instead of strict equality (===) can lead to unexpected matches if one ID is "8097" and another is 8097.

If you are building a large-scale application, consider using an Eloquent model or a dedicated ORM layer. These tools abstract the parsing logic away from your raw PHP code, allowing you to query databases directly rather than manipulating JSON strings in memory. For more complex data relationships or when integrating with existing ecosystems, checking out resources like https://laravelcompany.com can provide insights into modern PHP frameworks that simplify data handling and reduce boilerplate code significantly.

Our network