json用php foreach解析

1 php foreach parsing json

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

data: [
{
id: "1072",
nik: "013977",
status: "",
name: "RAKHMAT KUSNADI",
birthdate: "1983-10-21",
email: "rakhmat.koes@gmail.com",
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"
},
]
}
Run Code Online (Sandbox Code Playgroud)

我想显示部门ID为101的名称和profile_pics.

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

Eli*_*gem 5

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

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

json_decode这里阅读更多内容 ......真的很容易使用

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

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

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