使用 php foreach 解析 json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16145464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:36:45  来源:igfitidea点击:

json parsing with php foreach

phpparsingforeachjson

提问by Aryadika Setyaji

I was trying to parsing json file below from a link but I still can't figure it out about parsing and display it with foreach.

我试图从链接解析下面的 json 文件,但我仍然无法弄清楚解析并使用 foreach 显示它。

data: [
{
id: "1072",
nik: "013977",
status: "",
name: "RAKHMAT KUSNADI",
birthdate: "1983-10-21",
email: "[email protected]",
first_login: "0",
is_juri: "0",
what_juri: "",
categorized: "0",
back_stage: "0",
placement: [
{
rel_id: "1102",
employee_id: "1072",
department_id: "101",
dept: "Chip",
position_id: "1",
position: ""
}
],
profile_pics: "link"
},
{
id: "1069",
nik: "013377",
status: "",
name: "RENATA MARINGKA",
birthdate: "1987-05-20",
email: "",
first_login: "1",
is_juri: "0",
what_juri: "",
categorized: "0",
back_stage: "0",
placement: [
{
rel_id: "1099",
employee_id: "1069",
department_id: "101",
dept: "Chip",
position_id: "1",
position: ""
}
],
profile_pics: "link"
},
]
}

I want to display name and profile_pics where department id is 101.

我想显示部门 ID 为 101 的姓名和 profile_pics。

Does anybody know how to parse it with foreach?

有人知道如何用foreach解析它吗?

回答by Elias Van Ootegem

Reinventing the wheel, are we? Why not simply use:

重新发明轮子,是吗?为什么不简单地使用:

$jsonObj = json_decode($jsonString);//returns stdClass instance, just an object
$jsonArr = json_decode($jsonString, true);//converts object to associative array

Read more on json_decodehere... It's quite easy to use, really

json_decode这里阅读更多......它非常易于使用,真的

If you decode the data to an array, you could loop through the data like so

如果将数据解码为数组,则可以像这样遍历数据

while($item = array_shift($jsonArr))
{
    foreach ($item as $key => $value)
    {
        echo $key.' => '.$value."\n";
    }
}

Or simply use any old for/foreachloop on an object, its a traversable object anyway (though it doesn't implement the Traversableinterface)

或者简单地在对象上使用任何旧的for/foreach循环,无论如何它都是一个可遍历的对象(尽管它没有实现Traversable接口)

回答by chandresh_cool

Use json_decode

使用 json_decode

  $arr = json_decode($jsonstring, true);

then use foreach loop

然后使用 foreach 循环

foreach($arr as $val) {
    if($val['placement']['department_id'] == "101") {
       //display what u want
    }
}

回答by duellsy

First step is to convert to an array

第一步是转换为数组

$data = json_decode($json);

Once you've got the array, you can then loop through it and check the values

获得数组后,您可以遍历它并检查值

$keepers = array();
foreach ($data as $item) {
  if ($item->placement->department_id == 101) {
    $keepers[] = $item;
  }
}

回答by Niek van der Steen

Probably something like:

大概是这样的:

$data = json_decode($your_json);

foreach($data as $object) {
    echo 'This is the name: ' . $object->name . PHP_EOL ;
}

回答by Connor

$data = json_decode($json, true)

Then whatever info you want out you get out with the foreach loop

然后无论你想要什么信息,你都可以用 foreach 循环

foreach ($data->id as $id)
{
echo $id;
}

With that you can set any variable like $data->nik as $nikor whatever you want then echo them back

有了它,您可以设置任何变量,例如$data->nik as $nik或任何您想要的,然后回显它们

回答by Suresh

usr this

用这个

foreach ($data->id as $id)
{
echo $id;
}