我正在嘗試更改PHP腳本,以便它可以接收JSON對象並使用給定的JSON對象。一切工作正常,直到我嘗試從for循環轉換從JSON陣列的對象(又名'東西')。PHP JSON解碼
什麼我錯在這裏做:
$json = '{
"foo": "hi",
"bar": "bye"
"stuff": [{"widget":"dd"},{"thing":"cc"},{"wcha":"dd"}]
}';
$arr = json_decode($json, true);
$foo = $arr['foo']; //works fine
$bar = $arr['bar']; //works fine
//old way that worked:
//$stuff = array("widget" => "dd", "thing" => "cc", "wcha" => "dd");
//new way that does not work:
$stuff = $arr['stuff'];
...
//This is where the problem is:
foreach ($stuff as $key => $value){...
在for循環的問題是,$關鍵是一個整數(不是實際值)和$值是單詞「陣列」(不實際值)。
看看你的結構,看看你如何試圖訪問它。 $ stuff是一個對象數組(或者我認爲它將被轉換爲PHP的一個數組數組)。 – Corbin 2012-04-26 21:45:38
你的JSON解碼給我NULL(沒有什麼工作正常):http://codepad.org/meAoAn0a – hakre 2012-04-26 22:28:59