我有一些这样的 json 数据,现在我想计算所有节点“b”,如果给它们编号,如何获取指定的节点。
[
{
"a":[
{
"b":"aaa" //in case this is the first node "b", definition number 1
},
{
"b":"bbb" //this is the second node "b", definition number 2
}
]
},
{
"a":[
{
"b":"ccc" //this is the third node "b", definition number 3
},
{
"b":"ddd" //this is the forth node "b", definition number 4
}
]
},
{
"c":"eee"
},
]
现在在示例中,有 4 个节点“b”,如何计算它们?以及如何在 php 代码中获取第三个节点“b”?
$json=json_decode($txt);
foreach($json as $data){
if($data->a){
foreach($data->a as $row){
echo $row->b.'<br />';
//count($row->b);
}
}
}