【问题标题】:Php json decoding with multiple objectsphp json解码与多个对象
【发布时间】:2016-06-29 22:43:41
【问题描述】:

Json

{
    "articles": [
        {
            "id": 1,
            "name": "Josh" 
        },
        {
            "id": 2,
            "name": "Jenny"
        },
        {
            "id": 3,
            "name": "Chris"
        }
    ]
}

如何通过 id 搜索姓名?
如果我只想选择 Josh,我该怎么办?

现在我正在使用 php 和 foreach 循环解码 json。

$url = "articles.json";
$json = json_decode(file_get_contents($url));
foreach($json->articles as $articles){
    echo $articles->name;
}

我只想选择 id 为 1 的名称。

【问题讨论】:

  • 你必须做一个forloop。如果你想搜索一种查询,你需要使用像 jql、jsonpath 这样的框架,或者更大的框架,里面有 json 查询。

标签: php html json api decode


【解决方案1】:
$json = '{
    "articles": [
        {
            "id": 1,
            "name": "Josh" 
        },
        {
            "id": 2,
            "name": "Jenny"
        },
        {
            "id": 3,
            "name": "Chris"
        }
    ]
}';

$json = json_decode($json);
$id = '1';
$result = 'NOT FOUND';
foreach($json->articles as $articles){
    if($articles->id == $id){
        $result = $articles;
    }
}

print_r($result);

输出:

stdClass 对象([id] => 1 [name] => Josh)

这样您就可以同时保持idname 绑定。

正常访问,如下:echo $result->name;

【讨论】:

    【解决方案2】:

    您可以使用array_filter 来获取数组中具有您想要的 id 的每个项目。

    $id = 1;  // define your id
    
    // filter for that id
    $results = array_filter($json->articles, function($x) use ($id) {
        return $x->id == $id;
    });
    

    结果将是一个包含零个或多个元素的数组,具体取决于找到 id 的次数。我不确定你需要对结果做什么,但你可以用 foreach 循环它们,或者像这样通过键访问特定元素:

    echo $results[0]->name;   // echo the name of the first match
    

    【讨论】:

      【解决方案3】:
          $url = "articles.json";
          $json = json_decode(file_get_contents($url));
          foreach($json->articles as $articles){
              if($articles->id==1){
                 echo "found id equal to 1";
          }
              if($articles->name=="Josh"){
                 echo "found Josh";
          }
          }
      

      作为fieldValueidname)的小函数,将urlcheckValue 作为附加参数会更好

         Function loopThruJSON ($json,$fieldValue,$checkValue) {
              $json = json_decode($json);
              foreach($json->articles as $articles){
                  if($articles->{$fieldValue}==$checkValue){
                     return "found ".$fieldValue." equal to ".$checkValue;
                  } else {
                     return false;
                  }
              }
          }
      

      基于FirstOne'scomment + example 以上可以写

          Function loopThruJSON ($json,$fieldValue,$checkValue) {
                  $json = json_decode($json);
                  foreach($json->articles as $articles){
                      if($articles->$fieldValue==$checkValue){
                         return "found ".$fieldValue." equal to ".$checkValue;
                      } else {
                         return false;
                      }
                  }
           }
      // Usage
      echo loopThruJSONID ("articles.json",'id',1) ;
      echo loopThruJSONName ("articles.json",'name',"Josh") ;
      

      PHP sandbox example, 3v4l example

      【讨论】:

      • 那行不通(函数部分)...当您调用该函数时,已经没有$articles->id...
      • @FirstOne 你是对的,我到底在写什么:) 一会儿
      • @FirstOne 已删除,请相应地编辑答案,谢谢。 :)
      【解决方案4】:

      这很简单:

      function findById($articles, $id){
          foreach($articles as $article){
              if($article['id'] === $id)
                 return $article;
          }
      }
      

      在此答案中,您需要将true 作为json_decode() 的第二个参数传递,以便将数据转换为关联数组。

      $json = json_decode(file_get_contents($url), true);
      

      【讨论】:

      • 附注,您也可以通过将$article['id'] 更改为$article->id 来调用它作为对象(在json_decode 中没有true)。
      • @FirstOne 是的,我知道,但我发现另一个更容易阅读,我不喜欢那个不知道为什么
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 2016-07-22
      • 2018-04-02
      • 2020-07-01
      • 2016-03-19
      • 2013-07-27
      • 2011-02-03
      相关资源
      最近更新 更多