[英]how to parse this kind of json array
I have a json array like 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"
}
}
i removed an element 'Piranha the Fish' With (UNSET) My json code changed to blow: 我删除了一个元素'食人鱼鱼'(UNSET)我的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"
}
}
when I remove element the counter of the element change to 0,1,3 so now i can't to have full access in my json elements 当我删除元素时,元素的计数器变为0,1,3所以现在我不能在我的json元素中拥有完全访问权限
I want echo value with this code : 我想用这段代码回声值:
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/>";
}
out put : 出局:
Name :Pussy the Cat
Age :5
Name :Roxy the Dog
Age :8
Try using foreach
. 尝试使用
foreach
。
foreach ($json_cod as $item) {
echo "Name :";echo $item['name'];echo "<br/>";
echo "Age :";echo $item['age'];echo "<br/>";
}
如果要使用代码进行回显,则在从数组中删除值后,使用$newarray=array_values($oldarray)
重新索引数组。
That is because unset()
remove the value along with the key/index from the array and left it as it is,ie it does not rearrange the key/index. 这是因为
unset()
从数组中删除了值和键/索引,并保持原样,即它不重新排列键/索引。
If you want to rearrange key the use array_slice()
instead See array_slice . 如果要重新排列键,请使用
array_slice()
而不是使用array_slice 。 and you will get what you want. 你会得到你想要的东西。 You will be able to do
你将能够做到
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/>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.