我正在尝试更改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