我正在尝试更改 PHP 脚本,以便它可以接收 JSON 对象并使用给定的 JSON 对象。一切正常,直到我尝试使用 for 循环遍历转换后的 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 循环中的问题是 $key 是一个整数(不是实际值),而 $value 是单词“数组”(不是实际值)。