【发布时间】:2017-03-04 06:10:48
【问题描述】:
我正在尝试解码我在 javascript 中创建并发送到 php 的对象数组,但是,我似乎无法正确解码它。 带有对象的数组如下所示:
我用来获取信息的 php 代码是这样的:
$allInformation = $_POST['information'];
//this works
$user = $allInformation[count($allInformation)-2];
$num = $allInformation = [count($allInformation)-1];
//this doesnt work
$array = var_dump(json_decode($allInformation));
数组 1 和 2 的索引工作得很好,但是,我似乎无法解码作为对象的索引 0。我该怎么办?
【问题讨论】:
-
你是怎么寄的?可能你需要
JSON.stringify信息,然后做json_decode($_POST['information']); -
提供更多信息,例如预期结果是什么错误?
-
我使用 ajax 以以下格式发送它:
var data = [];在我的循环中收集数据,我使用数据数组插入以下内容:data[i] = {'user': username, 'date':date}。然后我将数据按原样传递给 ajax 以发布它。 -
使用 jQuery 吗?所以你序列化它不是json,只是 var_dump($allInformation)
-
这是我使用
$file = json_decode($_POST['information']); echo $file[0]["user"];输出的输出:<br /> <font size='1'><table class='xdebug-error xe-fatal-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'> <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Cannot use object of type stdClass as array in ...
标签: javascript php jquery arrays object