简体   繁体   English

限制循环中的项目数量

[英]Limit the amount of items in a loop

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.我想将数组大小限制为不超过 500 个项目。

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?我已经查看了这些 SO 问题: PHP: Limit foreach() statement? Limit this PHP foreach statement to just 5 loops ...but haven't had success in limiting the $terms array. 将此 PHP foreach 语句限制为仅 5 个循环......但在限制 $terms 数组方面没有成功。

If you have any ideas about a way to limit $terms[] to no more than 500 terms, please let me know.如果您对将 $terms[] 限制为不超过 500 个术语的方法有任何想法,请告诉我。

++ Updated code for @Barmar: ++ @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);
}

You're slicing the wrong array, it should be $tree .你切片错误的数组,它应该是$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);
}

First of all, it is not logical to load a huge amount of data and then process and limit them in php.首先,在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.最佳实践是在 mySql 查询中limit它们。 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:如果您想以任何方式完成这项工作,有几种方法可以为您完成这项任务:使用array_slice是第一个选项:

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

Second option is using while for your loop and not foreach .第二个选项是使用while循环而不是foreach I don't know anything about the content of $tree , but lets suppose it is an array like the following:我对$tree的内容一无所知,但假设它是一个如下所示的数组:

$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.在这种情况下,使用while循环限制要从该数组中取出的项目将非常容易。 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!相当容易!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM