我有一个JSON对象,我试图迭代来计算对象内有多少值。
我的JSON;
[{
"accountId": "1746756959",
"containerId": "7454209",
"containerVersionId": "7",
"container": {
"accountId": "1746756959",
"containerId": "7454209"
},
"tag": [{
"accountId": "1746756959",
"name": "Universal Analytics",
"parameter": [{
"key": "overrideGaSettings",
"type": "boolean",
"value": "false"
}]
}, {
"accountId": "1746756959",
"parameter": [{
"key": "detectTitle",
"type": "boolean",
"value": "true"
}]
}, {
"accountId": "1746756959",
"name": "contentEngagement",
"parameter": [{
"key": "html",
"type": "template",
"value": "<script>\n\n<\/script>"
}]
}],
"trigger": [{
"accountId": "1746756959",
"name": "Trigger 1",
"workspaceId": null
}, {
"accountId": "1746756959",
"name": "Trigger 2",
"workspaceId": null
}, {
"accountId": "1746756959",
"containerId": "7454209",
"fingerprint": "1507893475158",
"name": "Trigger 3",
"notes": null,
"parentFolderId": null,
"path": null,
"tagManagerUrl": null,
"triggerId": "13",
"type": "windowLoaded",
"workspaceId": null
}],
"variable": [{
"accountId": "1746756959",
"containerId": "7454209",
"disablingTriggerId": null,
"enablingTriggerId": null,
"fingerprint": "1505216399119",
"name": "Google Analytics Settings",
"notes": null,
"parentFolderId": null,
"path": null,
"scheduleEndMs": null,
"scheduleStartMs": null,
"tagManagerUrl": null,
"type": "gas",
"variableId": "1",
"workspaceId": null,
"parameter": [{
"key": "cookieDomain",
"type": "template",
"value": "auto"
}]
}]
}]
当我尝试迭代并打印出值时,我可以这样做,但是当我计算它时,我会得到(1 1 1)
的结果,应该有(3)
。我怀疑它与foreach有关。我的PHP代码如下所示:
foreach($LiveContainer as $item) {
foreach ($item['tag'] as $accountPath) {
echo count($accountPath->name); echo "<br>";
}}
结果:
1 1 1
我想要的是计算tag
中有多少项。
PS:我从JSON中删除了很多,因为它很长,maby“名称”并非在所有标签中都可用,对不起
由于
答案 0 :(得分:1)
foreach($LiveContainer as $item) {
echo count($item['tag']); echo "<br>";
}