gpt4 book ai didi

javascript - 如何在 php 中获取包含一个数组对象的 serializableArray jQuery?

转载 作者:行者123 更新时间:2023-11-30 00:17:16 25 4
gpt4 key购买 nike

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

我有这个:

console log of array structure

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

我尝试使用 dump 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' => string '15/12/2015' (length=10) public 'descDespesa' => 字符串 'teste1' (length=6) public 'budgetDespesa' => string '001 001 0E2R' (length=12) public 'valorDespesa' => string '2133.33' (length=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;

希望这对您有所帮助。

关于javascript - 如何在 php 中获取包含一个数组对象的 serializableArray jQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34301544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com