gpt4 book ai didi

php - 如何在 json 中传递变量数组并检索它

转载 作者:行者123 更新时间:2023-11-30 00:29:08 24 4
gpt4 key购买 nike

我得到了一个在变量 $coinPacks 中命名的数组,我尝试像这样传递它

<小时/>
echo $this->_helper->json(array('pack'=>$coinPacks));
<小时/>

但是当我使用此代码显示它时什么也没有

<小时/>
 alert(JSON.stringify(response.pack));
<小时/>

如何传递值并检索它

<小时/>

当我打印变量 $CoinPacks 时,我得到这样的响应

[_data:protected] => 数组 ( [0] => 数组 ( [id] => 1 [硬币数量] => 100 [价格] => 1.00 )

        [1] => Array
(
[id] => 2
[number_of_coins] => 500
[price] => 3.00
)

[2] => Array
(
[id] => 3
[number_of_coins] => 1000
[price] => 5.00
)

[3] => Array
(
[id] => 4
[number_of_coins] => 5000
[price] => 10.00
)

)

也喜欢这样的一些代码

[_metadata:protected] => 数组 ( [id] => 数组 ( [SCHEMA_NAME] => [TABLE_NAME] => 包 [COLUMN_NAME] => ID [COLUMN_POSITION] => 1 [DATA_TYPE] => 整数 [默认] => [可以为空] => [长度] => [规模] => [精度] => [未签名] => [主要] => 1 [主要位置] => 1 [身份] => 1 )

                [pack_name] => Array
(
[SCHEMA_NAME] =>
[TABLE_NAME] => packs
[COLUMN_NAME] => pack_name
[COLUMN_POSITION] => 2
[DATA_TYPE] => varchar
[DEFAULT] =>
[NULLABLE] => 1
[LENGTH] => 200
[SCALE] =>
[PRECISION] =>
[UNSIGNED] =>
[PRIMARY] =>
[PRIMARY_POSITION] =>
[IDENTITY] =>
)

[number_of_coins] => Array
(
[SCHEMA_NAME] =>
[TABLE_NAME] => packs
[COLUMN_NAME] => number_of_coins
[COLUMN_POSITION] => 3
[DATA_TYPE] => int
[DEFAULT] =>
[NULLABLE] => 1
[LENGTH] =>
[SCALE] =>
[PRECISION] =>
[UNSIGNED] =>
[PRIMARY] =>
[PRIMARY_POSITION] =>
[IDENTITY] =>
)

[price] => Array
(
[SCHEMA_NAME] =>
[TABLE_NAME] => packs
[COLUMN_NAME] => price
[COLUMN_POSITION] => 4
[DATA_TYPE] => float
[DEFAULT] =>
[NULLABLE] => 1
[LENGTH] =>
[SCALE] => 2
[PRECISION] => 10
[UNSIGNED] =>
[PRIMARY] =>
[PRIMARY_POSITION] =>
[IDENTITY] =>
)

最佳答案

PHP

json_encode(array('pack'=>$coinPacks)); // check if it matches the output of your helper

这会将数组编码为 JSON。

<小时/>

JS

console.log(JSON.parse(response));

这会将 JSON 解码为 JS 对象(并将其输出到控制台)。

要获取您的包信息:

var pack = JSON.parse(response).pack;

关于php - 如何在 json 中传递变量数组并检索它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22651769/

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