5 回答

TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊
要迭代多維數(shù)組,可以使用RecursiveArrayIterator
$jsonIterator = new RecursiveIteratorIterator( new RecursiveArrayIterator(json_decode($json, TRUE)), RecursiveIteratorIterator::SELF_FIRST);foreach ($jsonIterator as $key => $val) { if(is_array($val)) { echo "$key:\n"; } else { echo "$key => $val\n"; }}
輸出:
John:status => WaitJennifer:status => ActiveJames:status => Activeage => 56count => 10progress => 0.0029857bad => 0

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊
最優(yōu)雅的解決方案:
$shipments = json_decode(file_get_contents("shipments.js"), true);
print_r($shipments);
請記住,json文件必須以UTF-8編碼而不使用BOM。如果文件有BOM,則json_decode將返回NULL。
或者:
$shipments = json_encode(json_decode(file_get_contents("shipments.js"), true));
echo $shipments;

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
沒有人指出你開始的“標(biāo)簽”是錯(cuò)誤的,完全超出我的范圍。您正在使用{}創(chuàng)建對象,而可以使用[]創(chuàng)建數(shù)組。
[ // <-- Note that I changed this
{
"name" : "john", // And moved the name here.
"status":"Wait"
},
{
"name" : "Jennifer",
"status":"Active"
},
{
"name" : "James",
"status":"Active",
"age":56,
"count":10,
"progress":0.0029857,
"bad":0
}
] // <-- And this.
通過此更改,json將被解析為數(shù)組而不是對象。使用該數(shù)組,您可以執(zhí)行任何操作,例如循環(huán)等。
- 5 回答
- 0 關(guān)注
- 2059 瀏覽
添加回答
舉報(bào)