How to search through a JSON Array in PHP

how to search data in json file using php
how to get data from json array in php
php search multidimensional array for multiple values
php search array of objects
array search php
php array search multiple values
json decode php
get specific key value from array php

I have a JSON array

{
  "people":[
    {
      "id": "8080",
      "content": "foo"
    },
    { 
      "id": "8097",
      "content": "bar"
    }
  ]
}

How would I search for 8097 and get the content?

Use the json_decode function to convert the JSON string to an array of object, then iterate through the array until the desired object is found:

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

$json = json_decode($str);
foreach ($json->people as $item) {
    if ($item->id == "8097") {
        echo $item->content;
    }
}

search within json with php, Once you have this array, you can loop through it to find the value you want: foreach($jsonArray["Results"] as $user) { if($user['username']  Decoding JSON data using PHP. You can parse or read JSON data from JSON object or any JSON file using PHP json_decode() method. Different examples of parsing JSON data using PHP are given below. Example-1: In the following example, JSON data is assigned in a variable and PHP json_decode() method is used to read the data in PHP format.

json_decode() it and treat like any other array or StdClass object

$arr = json_decode('{
  "people":[
    {
      "id": "8080",
      "content": "foo"
    },
    { 
      "id": "8097",
      "content": "bar"
    }
  ]
}',true);

$results = array_filter($arr['people'], function($people) {
  return $people['id'] == 8097;
});


var_dump($results);

/* 
array(1) {
  [1]=>
  array(2) {
    ["id"]=>
    string(4) "8097"
    ["content"]=>
    string(3) "bar"
  }
}
*/

How to search JSON in PHP? - PHP, So if you want to search for a key, then get array keys and then search in them. thanks it works: $keys = array_keys($folders); $needle = array($params['mailbox']); var_dump((bool)array_intersect($keys, $needle)); The point is that if you have a JSON Array (or an Array of like Objects) and have to search for a key value on an Object inside that Array (more than once), it is far more efficient to create a map of the key or keys you wish to search on, than to search the Array each time. Using this technique will improve the performance of your application

If you have a fairly small number of "people" objects, then the previous answers will work for you. Given that your example has IDs in the 8000 range, I suspect looking at every single ID might not be ideal. So here is another method that will examine far fewer people before finding the right one (as long as the people are in order of ID):

//start with JSON stored as a string in $jsonStr variable
//  pull sorted array from JSON
$sortedArray = json_decode($jsonStr, true);
$target = 8097; //this can be changed to any other ID you need to find
$targetPerson = findContentByIndex($sortedArray, $target, 0, count($sortedArray));
if ($targetPerson == -1) //no match was found
    echo "No Match Found";


function findContentByIndex($sortedArray, $target, $low, $high) {
    //this is basically a binary search

    if ($high < low) return -1; //match not found
    $mid = $low + (($high-$low) / 2)
    if ($sortedArray[$mid]['id'] > $target) 
        //search the first half of the remaining objects
        return findContentByIndex($sortedArray, $target, $low, $mid - 1);
    else if ($sortedArray[$mid]['id'] < $target)
        //search the second half of the remaining objects
        return findContentByIndex($sortedArray, $target, $mid + 1, $high);
    else
        //match found! return it!
        return $sortedArray[$mid];
}

PHP array_search() Function, Example. Search an array for the value "red" and return its key: <?php $a=  Convert the request into an object, using the PHP function json_decode (). Access the database, and fill an array with the requested data. Add the array to an object, and return the object as JSON using the json_encode () function. Loop Through the Result

PHP and JSON, Example. This example decodes JSON data into a PHP associative array: <?php This example shows how to loop through the values of a PHP object: <?php While it works for non-arrays, I can't for the life of me figure out how to print all the values within the "Reviews" Array. What I would like to do is to loop through this response, probably with forreach(), resulting in a list containing the rating and excerpt for each review in the response. Any guidance / direction is greatly appreciated..

How to create an array for JSON using PHP?, These key will be a string or an integer which will be used as an index to search the corresponding value in the array. The json_encode function is used to  PHP provides a json_encode() function that converts PHP arrays into JavaScript. Technically, it is in JSON format. JSON stands for JavaScript Object Notation. Statement: If you have a PHP array and you need to convert it into the JavaScript array so there is a function provided by PHP that will easily convert that PHP array into the JavaScript

Dealing with JSON arrays and objects in PHP, We need to tell PHP that the empty object is explicitly an object, not an array. To define this query in PHP, you would do: $params['body']  json_encode() function will help you to encode array to JSON in php. if you will use just json_encode function directly without any specific option, it will return an array. . Like mention above ques

Comments
  • can create a loop to go through peope->id array
  • How many people are represented? If it is sufficiently small, then one of the search loops presented below could work well. If it is very large, you might need something else.
  • Also, are the entries always in increasing order of id? If so, an algorithm built around that could produce something much more efficient than looping through every entry.
  • I totally forgot to do the ->people part every time. works perfectly.
  • I think you have the arguments for array_map out of order.
  • I used array_map instead of array_filter. Fixed now.