Krylus Posted May 5, 2014 Share Posted May 5, 2014 Hi, I have very little experience with php programming. I have a rather big array i want to convert to json using json_encode echo json_encode($data_array); However, I am getting the following error: <b>Fatal error</b>: Allowed memory size of 67108864 bytes exhausted (tried to allocate 4194384 bytes) I assume the array i am trying to convert is too big for server to handle. Is there some way how I can accomplish this, perhaps iteratively? Thanks for answer, Krylus Quote Link to comment https://forums.phpfreaks.com/topic/288266-json_encode-exceeds-allowed-memory/ Share on other sites More sharing options...
QuickOldCar Posted May 5, 2014 Share Posted May 5, 2014 You can set the memory limit at the top of your script like this ini_set('memory_limit', '-1'); set_time_limit(0); That is unlimited memory and also unlimited timeout if need that too. Quote Link to comment https://forums.phpfreaks.com/topic/288266-json_encode-exceeds-allowed-memory/#findComment-1478305 Share on other sites More sharing options...
Solution requinix Posted May 5, 2014 Solution Share Posted May 5, 2014 Upping the memory limit (I'd suggest picking a limit, rather an unbounding it) would be the easiest solution if you know that the array won't get even larger. Otherwise you can construct some of the JSON yourself. How are you building $data_array? If you're using a loop like $data_array = array(); for ($i = 0; $i < 10000000; $i++) { $data_array[] = $i; } echo json_encode($data_array);then you can output parts of it inside the loop rather than all at once after. echo "["; // or { for an associative array (don't forget to output the json_encode()d keys and their colons) $first = true; for ($i = 0; $i < 10000000; $i++) { // json does not support trailing commas like php does // and you can't do an implode(",", $array) here because of the memory usage if ($first) { $first = false; } else { echo ","; } echo json_encode($i); } echo "]"; // or } Quote Link to comment https://forums.phpfreaks.com/topic/288266-json_encode-exceeds-allowed-memory/#findComment-1478307 Share on other sites More sharing options...
Krylus Posted May 5, 2014 Author Share Posted May 5, 2014 (edited) I tried increasing the memory limit as suggested by QuickOldCar earlier, however this does not work on the server where my data is stored (I suppose it is disabled?). Nevertheless, requinix’s solution with building JSON by myself worked. Guess I should have thought of that . Thank you! Edited May 5, 2014 by Krylus Quote Link to comment https://forums.phpfreaks.com/topic/288266-json_encode-exceeds-allowed-memory/#findComment-1478316 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.