Home > Software design >  Limit the amount of items in a loop
Limit the amount of items in a loop

Time:09-22

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);
}

CodePudding user response:

You're slicing the wrong array, it should be $tree.

public function deleteTermByVid($vid) {
  $terms = [];
  $controller = $this->entityTypeManager->getStorage('taxonomy_term');
  $tree = $controller->loadTree($vid);
  $tree = array_slice($tree, 0, 5);
  foreach ($tree as $term) {
    $terms[] = $term->tid;
  }
  $entities = $controller->loadMultiple($terms);
  $controller->delete($entities);
  return count($terms);
}

CodePudding user response:

First of all, it is not logical to load a huge amount of data and then process and limit them in php. It makes the task slower, and wastes the server resources a little bit more. The best practice is to limit them in your mySql query. If you want to do the job by any means, there are a couple of ways possible to do the task for you: Using array_slice is the first option:

 $tree = array_slice($tree, 0, 5);

Second option is using while for your loop and not foreach. I don't know anything about the content of $tree, but lets suppose it is an array like the following:

$tree = ["A", "B", "C", "D", "E", "F"];

In this case it will be really easy to limit the items you want to get out of this array using while loop. Pay attention to the following:

$i = 0;
while ($i < 3)
{
    // Do whatever you wish here...
    echo $tree[$i] .PHP_EOL;
    $i  ;
}

In this scenario, you can get just three items out of your array. Pretty easy!

  •  Tags:  
  • php
  • Related