3v4l.org

run code in 300+ PHP versions simultaneously
<?php function batchDelete(): \Generator { // $entityManager = $this->getEntityManager($class); $queryResults = range(0, 102); $i = 0; $removedObjects = []; foreach ($queryResults as $pos => $object) { // $entityManager->remove($object[0]); $removedObjects[] = $object; if (0 === (++$i % 20)) { yield from $removedObjects; $removedObjects = []; // $entityManager->flush(); // $entityManager->clear(); } } yield from $removedObjects; // $entityManager->flush(); // $entityManager->clear(); } function callerFunctionBatchDelete() { foreach (batchDelete() as $deletedObject) { // Fire any event you may need regarding `$deletedObject`. echo 'Some operation with object "'.$deletedObject."\".\n"; } } callerFunctionBatchDelete();

preferences:
26.74 ms | 404 KiB | 5 Q