PHP对内存的运行,在PHP中运行密集的批处理过程,并避免内存耗尽

我有几千条记录(存储在MYSQL表中的表中),我需要“批处理”.所有记录都包含一个大型

JSON.在某些情况下,

JSON超过1MB(是的,我的数据库远远超过1GB).

我有一个功能,它抓取一条记录,解码JSON,更改一些数据,将PHP数组重新编码回JSON,并将其保存回数据库.很简单. FWIW,这是在CakePHP应用程序的上下文中.

给定一个ID数组,我试图做这样的事情(非常简单的模拟代码):

foreach ($ids as $id) {

$this->Model->id = $id;

$data = $this->Model->read();

$newData = processData($data);

$this->Model->save($newData);

}

问题是,很快PHP就会耗尽内存.当运行这样的foreach时,几乎就像PHP从一个记录移动到下一个记录,而不释放前面操作所需的内存.

无论如何以这样的方式运行一个循环,以便在继续循环的下一次迭代之前释放内存,这样我才能真正处理大量的数据?

编辑:添加更多代码.此函数接受我的JSON,将其转换为PHP数组,进行一些操作(即根据另一个数组中的内容重新配置数据),并替换原始数组中的值. JSON有很多层深,因此极长的foreach循环.

function processData($theData) {

$toConvert = json_decode($theData['Program']['data'], $assoc = true);

foreach($toConvert['cycles'] as $cycle => $val) {

foreach($toConvert['cycles'][$cycle]['days'] as $day => $val) {

foreach($toConvert['cycles'][$cycle]['days'][$day]['sections'] as $section => $val) {

foreach($toConvert['cycles'][$cycle]['days'][$day]['sections'] as $section => $val) {

foreach($toConvert['cycles'][$cycle]['days'][$day]['sections'][$section]['exercises'] as $exercise => $val) {

if (isset($toConvert['cycles'][$cycle]['days'][$day]['sections'][$section]['exercises'][$exercise]['selectedFolder'])) {

$folderName = $toConvert['cycles'][$cycle]['days'][$day]['sections'][$section]['exercises'][$exercise]['selectedFolder']['folderName'];

if ( isset($newFolderList['Folders'][$folderName]) ) {

$toConvert['cycles'][$cycle]['days'][$day]['sections'][$section]['exercises'][$exercise]['selectedFolder'] = $newFolderList['Folders'][$folderName]['id'];

}

}

if (isset($toConvert['cycles'][$cycle]['days'][$day]['sections'][$section]['exercises'][$exercise]['selectedFile'])) {

$fileName = basename($toConvert['cycles'][$cycle]['days'][$day]['sections'][$section]['exercises'][$exercise]['selectedFile']['fileURL']);

if ( isset($newFolderList['Exercises'][$fileName]) ) {

$toConvert['cycles'][$cycle]['days'][$day]['sections'][$section]['exercises'][$exercise]['selectedFile'] = $newFolderList['Exercises'][$fileName]['id'];

}

}

}

}

}

}

}

return $toConvert;

}

Model-> read()实际上只是告诉Cake从db中提取记录,并将其返回到数组中.幕后发生了大量的事情,更有知识的人必须解释这一点.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值