In addition to the for and foreach problems, you need to rebuild your solution. You fall into the limits of memory because you are legally using too much memory. Each time you unserialize the contents of a database column and store it in an array
$data[$key]['data']
PHP must set aside a piece of memory to store this data in order to subsequently access it. When your array gets too large, you have lost memory. In plain English, you speak PHP
Take all 5,000 rows of data and store them in memory, I'm going to do something with them later.
You need to think of another way to approach your problem. Listed below are two quick thoughts about the problem.
You cannot store elements in memory and just perform any actions you like in the loop, which allows php to drop elements as needed
foreach ($data as $key => $report) { $object = unserialize($report['serialized_values']);
You can also store the information you need from an unserialized object, rather than storing the entire object
foreach ($data as $key => $report) { $object = unserialize($report['serialized_values']); $data = array(); $data['foo'] = $object->foo; $data[$key]['data'] = $data; }
In short: you push memory limits because you are actually using too much memory. There is no magic solution. Storing serialized data and trying to load it all in one program is an intensive approach to memory, regardless of language / platform.
source share