Jump to content

json_encode exceeds allowed memory


Krylus

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
https://forums.phpfreaks.com/topic/288266-json_encode-exceeds-allowed-memory/
Share on other sites

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 }

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.