If the array keys in your PHP array are not consecutive numbers, json_encode()
must make the other construct an object since JavaScript arrays are always consecutively numerically indexed.
Use array_values()
on the outer structure in PHP to discard the original array keys and replace them with zero-based consecutive numbering:
Example:
// Non-consecutive 3number keys are OK for PHP
// but not for a JavaScript array
$array = array(
2 => array("Afghanistan", 32, 13),
4 => array("Albania", 32, 12)
);
// array_values() removes the original keys and replaces
// with plain consecutive numbers
$out = array_values($array);
json_encode($out);
// [["Afghanistan", 32, 13], ["Albania", 32, 12]]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…