且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将 PHP 数组编码为 JSON 数组,而不是对象?

更新时间:2023-01-12 11:46:50

您如何设置初始数组?

如果你这样设置:

array(
 "1" => array(...),
 "2" => array(...),
);

那么你没有一个带有数字索引的数组,而是字符串,在 JS 世界中它被转换为一个对象.如果您没有设置严格的顺序(即从 0 而不是 1 开始),也会发生这种情况.

then you don't have an array with numeric indexes but strings, and that's converted to an object in JS world. This can happen also if you don't set a strict order (i.e. starting at 0 instead of 1).

然而,这是在黑暗中拍摄,因为我看不到您的原始代码:首先尝试在不使用键的情况下设置您的数组:

This is a shot in the dark, however, because I can't see your original code: try setting your array without using keys at all in the first place:

array(
 array(...),
 array(...),
);