我怎样才能得到在php中包含一个数组对象的serializableArray jQuery?

How can I get serializableArray jQuery that contains one array object in php?

如何让我的对象数组在 jquery 到 php 之间序列化?

我有这个:

我需要在 php 中获取 jQuery(vetDespesas) 发送的这个数组来创建我的 sql 查询。

我尝试使用转储 php serialize, unserialize 但它不起作用。

我在 jQuery 中使用这个:

   // this = my Form html

    var dataSend = $(this).serializeArray(); // other datas...

    dataSend.push({name:'moeda',value:moeda});
    dataSend.push({name:'moedaCotacao',value:moedaCotacao});
    **dataSend.push({name:'vetDespesas',value:vetDespesas});** object array

在php中,我该如何编写代码?

$requisitadopor = $_POST['requisitadopor'];
$autorizadopor = $_POST['autorizadopor'];
$departamento = $_POST['departamento'];
$unidade = $_POST['unidade'];

var_dump($_POST['vetDespesas']);  // doesn't work =/ (array of objects)


vetDespesas = JSON.stringify(vetDespesas);

我php使用:

    $a = json_decode($_POST['vetDespesas']);
    var_dump($a[0]);

我的 chrome 控制台打印:

对象(stdClass)[2] public 'dataDespesa' => 字符串 '15/12/2015' (长度=10) public 'descDespesa' => 字符串 'teste1' (长度=6) public 'budgetDespesa' => 字符串 '001 001 0E2R' (长度=12) public 'valorDespesa' => 字符串 '2133.33' (长度=7)

我如何访问这些数据?

在将它发送到 PHP 之前,您应该将其转换为 JSON 字符串:

vetDespesas = JSON.stringify(vetDespesas);

要在 PHP 代码中获取对象,您应该使用 json_decode() :

$my_object = json_decode($_POST['vetDespesas']);

您可以使用 -> 访问 object(stdClass) 属性,例如:

echo $my_object->dataDespesa;
echo $my_object->budgetDespesa;

希望对您有所帮助。