5 回答

TA貢獻2003條經驗 獲得超2個贊
要迭代多維數組,可以使用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貢獻1797條經驗 獲得超4個贊
最優雅的解決方案:
$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貢獻1816條經驗 獲得超6個贊
沒有人指出你開始的“標簽”是錯誤的,完全超出我的范圍。您正在使用{}創建對象,而可以使用[]創建數組。
[ // <-- 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將被解析為數組而不是對象。使用該數組,您可以執行任何操作,例如循環等。
- 5 回答
- 0 關注
- 2081 瀏覽
添加回答
舉報