无法在Ubuntu中迭代对象PHP 5.2.10-2适用于PHP 5.2.10吗?

时间:2009-12-07 13:36:44

标签: php ubuntu versioning wamp

使用此代码,我将遍历一个对象。

使用:

  • 使用WAMP和PHP 5.2.9的Windows
  • 使用PHP 5.2.10的Linux Web服务器

它无法在我的桌面上运行:

  • Ubuntu 9.10 with PHP 5.2.10-2 from 回购的
$incomingData = json_decode($_POST['data']);

foreach($incomingData as $key => $action)
{

}
     

提供的参数无效   的foreach()

3 个答案:

答案 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字符串;它不会起作用。