How to search through a JSON Array in PHP

前端 未结 3 2008
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 08:05

I have a JSON array

{
  \"people\":[
    {
      \"id\": \"8080\",
      \"content\": \"foo\"
    },
    { 
      \"id\": \"8097\",
      \"content\": \"ba         


        
3条回答
  •  一整个雨季
    2020-12-01 08:55

    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"
      }
    }
    */
    

提交回复
热议问题