我有一个类似 blew 的 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 代码更改为 blow:
{
"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
最佳答案
尝试使用 foreach
。
foreach ($json_cod as $item) {
echo "Name :";echo $item['name'];echo "<br/>";
echo "Age :";echo $item['age'];echo "<br/>";
}
关于javascript - 如何解析这种json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38700192/