gpt4 book ai didi

PHP:将数组转换为 JSON

转载 作者:行者123 更新时间:2023-12-02 04:32:09 25 4
gpt4 key购买 nike

myArray 是:

Array
(
[1] => 0
[2] => 11970.99
[3] => 2888
[4] => 0
[5] => 1500
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
)

我想将其转换为 JSON,例如:

[{"name":"Recebimentos","data":[0,11970.99,2888,0,1500,0,0,0,0,0,0,0]}

我尝试过:

echo json_encode(array(
array(name=> 'Recebimentos', data=>$myArray),
));

但这正在返回:

[{"name":"Recebimentos","data":{"1":0,"2":11970.99,"3":2888,"4":0,"5":1500,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}}

最佳答案

您的问题是非序列索引。
Php 数组必须具有索引 0..array.length-1 才能编码为 JSON 数组。

您可以使用 array_values 重置数组键:

echo json_encode(array(
array(name=> 'Recebimentos', data=>array_values($myArray)),
));

关于PHP:将数组转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22792353/

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