【发布时间】:2016-12-06 14:43:07
【问题描述】:
我有一个类似自爆的 json 数组:
{
"0": {
"kind": "mammal",
"name": "Pussy the Cat",
"weight": "12kg",
"age": "5"
},
"1": {
"kind": "mammal",
"name": "Roxy the Dog",
"weight": "25kg",
"age": "8"
},
"2": {
"kind": "fish",
"name": "Piranha the Fish",
"weight": "1kg",
"age": "1"
},
"3": {
"kind": "bird",
"name": "Einstein the Parrot",
"weight": "0.5kg",
"age": "4"
}
}
我使用(未设置)删除了一个元素“Piranha the Fish”,我的 json 代码更改为:
{
"0": {
"kind": "mammal",
"name": "Pussy the Cat",
"weight": "12kg",
"age": "5"
},
"1": {
"kind": "mammal",
"name": "Roxy the Dog",
"weight": "25kg",
"age": "8"
},
"3": {
"kind": "bird",
"name": "Einstein the Parrot",
"weight": "0.5kg",
"age": "4"
}
}
当我删除元素时,元素的计数器变为 0,1,3 所以现在我不能完全访问我的 json 元素
我想用这段代码回显值:
for ($i = 0; $i < $JsonCount - 1; $i++) {
echo "Name :";
echo $json_cod[$i]['name'];
echo "<br/>";
echo "Age :";
echo $json_cod[$i]['age'];
echo "<br/>";
}
输出:
Name :Pussy the Cat
Age :5
Name :Roxy the Dog
Age :8
【问题讨论】:
-
您的数据结构看起来不像 JSON 数组。您确定这就是您要解析的内容吗?
-
使用
foreach索引可以忽略 -
在 PHP 中重新索引数组之前将其编码为 JSON;或使用developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
标签: javascript php arrays json web