简体   繁体   中英

stop array conversion to objects for large arrays

I retrieving data from backend to frontend from json, when I send small array It prints as array

[[["79.869594371948224,6.9351183796382223"],["79.869644126545694,6.9341375295332206"],.......

and when I send Large array it prints as an object

{"0":[["81.175402528806714,6.9255110868816949"],["81.1758192226863,6.9248754300773347"],........

I pass the same format result from the backend, and why this happens for large arrays? how to solve this?

If you are wondering why json_encode() encodes your PHP array as a JSON object instead of a JSON array, you might want to double check your array keys because json_encode() assumes that you array is an object if your keys are not sequential.

$foo = array('a','b','c');
print_r(json_encode($foo));
unset($foo[0]);
echo "\n";
print_r(json_encode($foo));
$foo = array_values($foo);
echo "\n";
print_r(json_encode($foo));

/** Output
["a","b","c"]
{"1":"b","2":"c"}
["b","c"]
*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM