使用此代码,我将遍历一个对象。
使用:
它无法在我的桌面上运行:
$incomingData = json_decode($_POST['data']); foreach($incomingData as $key => $action) { }
提供的参数无效 的foreach()
答案 0 :(得分:3)
也许您的某个服务器启用了magic_quotes_gpc,因此您可以在解码之前尝试在$ _POST ['data']上使用stripslashes。两个PHP版本都应该能够遍历对象。
答案 1 :(得分:2)
你确定你的PHP版本正确吗?
来自foreach
的文档:
从PHP 5开始,可以迭代 对象也是。
尝试使用json_decode
并将第二个参数设置为true
,以使json_decode
返回关联数组而不是对象。
$incomingData = json_decode($_POST['data'], true);
第二个参数$assoc
(默认为false
)表示:
当
TRUE
时,返回的对象将被转换为关联数组。
我的猜测是,一个盒子的价格低于PHP 5.
要确认这是问题所在,请尝试将$incomingData
更改为某种无关联的关联数组:
$incomingData = array("foo" => "bar", "baz" => "monkey");
并查看是否会导致错误消失。
答案 2 :(得分:0)
尝试做:
$data_array = get_object_vars(json_decode($json_data)); print_r($data_array); this is only if you obtain information from some web page such as $data = file_get_contents('http://www.someurl.com/somerestful_url/'); $data_array = get_object_vars(json_decode($data)); print_r($data_array);
另外,你可能试图做json_encode,而是把json_decode($ _ POST ['data']);
除非你在$ _POST ['data']中有json字符串;它不会起作用。