Reputation: 161
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!
Upvotes: 1
Views: 7924
Reputation: 161
The acceptable answer is this
$prefix = '';
echo '[';
foreach($rows as $row) {
echo $prefix, json_encode($row); $prefix = ',';
}
echo ']';
posted above.
However, it is still not guarantee to work if the dataset is too big.
The possible solutions are Limit the Query. Instead of using SELECT *. Just select which rows you need BUT in my case, I have no absolute control of the query. So this solution is not gonna work for me .
Last one is to upgrade our server's memory or even the server itself, to keep up with the continuously growing dataset.
Thanks all for your help!
Upvotes: 0
Reputation:
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.
Upvotes: 6