/**
* yield 生成器处理数组
* 减少内存开销
* @param $data
* @param int $siez
* @return \Generator
*/
function yieldData($data, $size = 0, $key = false)
{
if ($size > 0) {
// 大数据切割成多个小数组
$chunkData = array_chunk($data, $size, $key);
unset($data);
foreach ($chunkData as $chunk) {
foreach ($chunk as $item) {
yield $item;
}
unset($chunk);
}
unset($chunkData);
} else {
foreach ($data as $item) {
yield $item;
}
}
}
public function index()
{
$data = Db::name('综转明细')->select();
//使用yield生成器处理数据减少内存开销
$yieldData = $this->yieldData($data);
// $yieldData = $this->yieldData($data, 10,true);
foreach ($yieldData as $v) {
//其他逻辑处理
dump($v);
}
}
function arrayInBatches($bigArray, $batchSize)
{
$count = count($bigArray);
$batchCount = ceil($count / $batchSize);
for ($i = 0; $i < $batchCount; $i++) {
$batch = array_slice($bigArray, $i * $batchSize, $batchSize);
// Do some processing on $batch
yield $batch;
}
}
public function test()
{
$list = Db::table('dd_综转明细')->select();
$res = $this->arrayInBatches($list, 20);
foreach ($res as $v) {
dump($v);
}
}
网友评论