I'm trying to build an array of a type of data in my database. The problem is there are tens of thousands of items in the resultant array, which is too much for my system to handle.
I'd like to limit the array size to no more than 500 items.
My code is here:
public function deleteTermByVid($vid) {
$terms = [];
$controller = $this->entityTypeManager->getStorage('taxonomy_term');
$tree = $controller->loadTree($vid);
foreach ($tree as $term) {
$terms[] = $term->tid;
}
$entities = $controller->loadMultiple($terms);
$controller->delete($entities);
return count($terms);
}
I've reviewed these SO issues: PHP: Limit foreach() statement? Limit this PHP foreach statement to just 5 loops ...but haven't had success in limiting the $terms array.
If you have any ideas about a way to limit $terms[] to no more than 500 terms, please let me know.
++ Updated code for @Barmar:
public function deleteTermByVid($vid) {
$terms = [];
$controller = $this->entityTypeManager->getStorage('taxonomy_term');
$tree = $controller->loadTree($vid);
foreach ($tree as $term) {
$terms[] = $term->tid;
}
$terms = array_slice($terms, 0, 5); /* ____NEW LINE ___ */
$entities = $controller->loadMultiple($terms);
$controller->delete($entities);
return count($terms);
}