Lex Dacs Lex Dacs - 1 month ago 13x
JSON Question

JSON_ENCODE can't encode large array

I need help with encoding large array into JSON using PHP's json_encode(). The array is from a local Database. I am using JS to parse JSON. However, the PHP script that handles array to JSON format stops if the dataset is too big.
(i.e 100,000 Results). I tried to up my memory_limit to -1 and still does not help. Is it possible to encode a big array using PHP's json_encode()?

Please let me know if my question is too confusing.

Thank you all!


If each of the rows in your array are successfully encoded by json_encode, then take advantage of that fact by only encoding each row and echo'ing out the array structure yourself.

That is

$prefix = '';
echo '[';
foreach($rows as $row) {
  echo $prefix, json_encode($row);
  $prefix = ',';
echo ']';

If the nesting is more complex, then a bit more detailed technique needs to be employed, but this technique saved me when I encountered the problem.