JSON解析与PHP的foreach [英] json parsing with php foreach

查看:115
本文介绍了JSON解析与PHP的foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个链接解析json文件,但我仍然无法弄清楚解析和foreach显示它。

 < code $ data 

$ b id $ 1072 $ b $ n $ 013977
status $
name RAKHMAT KUSNADI,
birthdate:1983-10-21,
email:rakhmat.koes@gmail.com,
first_login:0,
is_juri: 0,
what_juri:,
分类:0,
back_stage:0,
展示位置:[
{
rel_id: 1102,
employee_id:1072,
department_id:101,
部门:Chip,
position_id:1,
职位:

$ $ b $ profile_pics:link
}
{
id:1069,
nik: 013377,
status:,
name:RENATA MARINGKA,
birthdate:1987-05-20,
email:,
first_login:1,
is_juri:0,
what_juri:,
分类:0,
back_stage:0,
放置:[
{
rel_id:1099,
employee_id:1069,
department_id:101,
dept:Chip ,
position_id:1,
position:
}
],
profile_pics:link
},
]
}

我想显示名称和profile_pics,其中部门ID是101。 p>

有没有人知道如何用foreach解析它?解析方案

重新创建轮子,我们?为什么不简单地使用:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b $ jsonArr = json_decode($ jsonString,true); //将对象转换为关联数组

阅读更多关于 json_decode 这里 ...这很容易使用,真的



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

 <$ c $ ($ item = array_shift($ jsonArr))
{
foreach($ item as $ key => $ value)
{
echo $ key。'= > 。$值 \\\







$ b $ p $或者干脆使用任何旧的对于 / foreach 循环对象,无论如何它是一个可遍历的对象(尽管它没有实现 Traversable interface)


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.

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"
},
]
}

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

Does anybody know how to parse it with foreach?

解决方案

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_decode here... It's quite easy to use, really

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/foreach loop on an object, its a traversable object anyway (though it doesn't implement the Traversable interface)

这篇关于JSON解析与PHP的foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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