如何通过PHP JSON数组搜索 [英] How to search through a JSON Array in PHP

查看:247
本文介绍了如何通过PHP JSON数组搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON数组

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

如何将搜索8097和获取内容?

How would I search for 8097 and get the content?

推荐答案

json_de code 功能应该帮助你:

The json_decode function should help you out:

$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;
    }
}

这篇关于如何通过PHP JSON数组搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆