gpt4 book ai didi

php - 使用ajax处理php双数组

转载 作者:行者123 更新时间:2023-12-01 03:11:23 26 4
gpt4 key购买 nike

我一直在试图弄清楚如何做到这一点——我想将一个数组(array())从PHP传递到jQuery。我以前曾使用单个数组完成过此操作,但从未使用过嵌套数组。

这是我的 PHP 代码:

$sector_one   = array('one' => 0, 'two' => 1, 'three' => 0, 'four' => 0);
$sector_two = array('one' => 0, 'two' => 1, 'three' => 0, 'four' => 0);
$sector_three = array('one' => 0, 'two' => 1, 'three' => 0, 'four' => 0);
$sector_four = array('one' => 0, 'two' => 1, 'three' => 0, 'four' => 0);

$array = [];
$array[0] = $sector_one;
$array[1] = $sector_two;
$array[2] = $sector_three;
$array[3] = $sector_four;

现在——我完全不知道如何处理它的数据方面,一旦它过去了jquery。我将如何在 jquery 中访问 $array[0]['one'] 中的信息?

最佳答案

只需使用 json_encode 对其进行编码

echo json_encode($array);

并在 jQuery 中将其取回

var array = jQuery.parseJSON(response);

// You can grab particular data like so:
console.log(array[0].one);

关于php - 使用ajax处理php双数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28521523/

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