Jump to content

json_encode exceeds allowed memory


Krylus
Go to solution Solved by requinix,

Recommended Posts

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

Link to comment
Share on other sites

  • Solution

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 }
Link to comment
Share on other sites

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 by Krylus
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.