这是JSON
{
"circuit_list": [
{
"_id": "58c0f378a986f808cdaf94cf",
"aggregation": {
"dev_name": "ME2-D2-BOO",
"port": {
"desc": "AKSES_SITE_SITE-TSEL_ME2-D2-BOO#1/2/5_200M_BOO082#CIPAKUBOO534",
"name": "1/2/5"
}
},
"area": "AREA 2",
"site_id": "N/A",
"site_name": "N/A"
},
{
"_id": "58c0f378a986f808cdaf94d0",
"aggregation": {
"dev_name": "ME2-D2-BOO",
"port": {
"desc": "AKSES_SITE_SITE-TSEL_ME2-D2-BOO#1/2/5_200M_BOO082#CIPAKUBOO534",
"name": "1/2/5"
}
},
"area": "AREA 2",
"site_id": "N/A",
"site_name": "N/A"
}
}
我已尝试使用此代码
$json = json_decode($url, true);
foreach($json as $value)
{
$_id = $value->_id;
}
它没有用。请帮助,我需要获取值以在视图上显示它们。我做错了吗?这个json很难,因为我不了解结构。 我通常用格式
解码json[{"id":"1","name":"faisal"}]
像这样和我的foreach一起工作。
答案 0 :(得分:3)
如果json_decode
的第二个参数为true,则该函数将返回一个数组而不是一个对象。此外,您需要遍历对象的circuit_list
属性。
$json = json_decode($url); // <- remove the parameter
foreach($json->circuit_list as $value) // <- loop over circuit_list
{
$_id = $value->_id;
}
答案 1 :(得分:2)
<?php
$json = json_decode($url,true);
foreach($json['circuit_list'] as $value)
{
$id = $value['_id'];
}
?>