【问题标题】:PHP Looping through half the arrayPHP循环遍历数组的一半
【发布时间】:2010-07-01 18:09:50
【问题描述】:

所以我有一个这样的数组:

foreach($obj as $element){
//do something
}

但如果数组包含超过 50 个元素(通常为 100 个),我只想循环遍历其中的前 50 个,然后中断循环。

【问题讨论】:

  • 我不明白这个问题。 foreach 循环只会遍历$obj 中存在的元素。无论$obj 的大小,您的意思是只有前 50 个吗?
  • 我猜他的意思是循环遍历数组的一半,无论它是什么大小。例如。如果数组包含 60 个元素,则循环遍历前 30 个元素。

标签: php arrays loops


【解决方案1】:

干净的方式:

$arr50 = array_slice($obj, 0, 50);
foreach($arr50 as $element){
    // $element....
}

普通方式(这仅适用于具有数字索引和 asc 顺序的数组):

for($i=0; $i<50 && $i<count($obj); $i++){
  $element = $obj[$i];
}

或者,如果您想使用foreach,您将必须使用计数器:

$counter = 0;
foreach($obj as $element){
  if( $counter == 50) break;
  // my eyes!!! this looks bad!
  $counter++;
}

【讨论】:

  • -1(虽然我真的不忍心真正否决你)提供了多种解决方案。
  • 大声笑...别担心。这就是SO的想法。无论如何...为什么是-1?
【解决方案2】:

循环播放一半。

for($i=0; $i<count($obj)/2; $i++)
{
  $element = $obj[$i];
  // do something
}

或者如果你总是想要前 50 个元素

$c = min(count($obj), 50);
for($i=0; $i<$c; $i++)
{
  $element = $obj[$i];
  // do something
}

【讨论】:

  • 如果数组包含 60 个元素,第一个 for 将不起作用。
  • 仅适用于整数键以 0 开头且键连续的数组。
【解决方案3】:

适用于任何数组,不仅适用于带有数字键0, 1, ...的数组:

$i = 0;
foreach ($obj as $element) {
    // do something
    if (++$i == 50) {
        break;
    }
}

【讨论】:

    【解决方案4】:

    一个简洁的替代方法是使用几个 SPL iterators 像:

    $limit = new LimitIterator(new ArrayIterator($obj), 0, 50);
    foreach ($limit as $element) {
        // ...
    }
    

    已经提到了相同的程序方法,请使用array_slice查看答案。

    【讨论】:

    • 不错的一个...感觉用JavaIterator接口!
    • 这对我来说是最好的遮阳篷。
    【解决方案5】:
    for ($i = 0, $el = reset($obj); $i < count($obj)/2; $i++, $el = next($obj)) {
        //$el contains the element
    }
    

    【讨论】:

    • 我从未见过有人在 1 个循环迭代器中安装三个函数:/
    【解决方案6】:

    保持简单

    $filtered = array_slice($array,0,((count($array)/2) < 50 && count($array) > 50 ? 50 : count($array)));
    //IF array/2 is les that 50- while the array is greater then 50 then split the array to 50 else use all the values of the array as there less then 50 so it will not devide
    foreach($filtered as $key => $row)
    {
      // I beliave in a thing called love.
    }
    

    这是怎么回事

    array_slice(
      $array, //Input the whole array
      0, //Start at the first index
      (
        count($array)/2 //and cut it down to half
      )
    )
    

    【讨论】:

    • 如果数组有 60 个元素怎么办?它只计算前 30 个。
    • 好的,我做了修改。继承人登录 IF ( (array / 2 is less than 50) then get a whole 50 else use all the array ) 很难解释,但应该可以。
    • 好的……那行得通。但是,为什么不直接使用array_slice($array, 0, 50)
    • 我没有得到任何线索,我只是想让他选择 x/2,所以我不知道,也许我错了:/
    【解决方案7】:
    for($i=0; $i < 50; $i++)
    {
      // work on $obj[$i];
    }
    

    【讨论】:

      【解决方案8】:

      这是对我来说最明显的方法:

      $i = 0;          // Define iterator
      
      while($obj[$i])  // Loop until there are no more
      {
       trace($obj[$i]); // Do your action
       $i++;           // Increment iterator
      }
      

      【讨论】:

      • 如果守卫中的任何元素评估为 false,这将提前结束。
      【解决方案9】:

      这应该适用于数组的一半,无论长度如何,也不管它是否有数字索引:

      $c = intval(count($array)/2);
      reset($array);
      foreach(range(1, $c) as $num){
          print key($array)." => ".current($array)."\n";
          next($array);
      }
      

      【讨论】:

        猜你喜欢
        • 2011-10-21
        • 2011-05-23
        • 2012-11-14
        • 2011-04-25
        • 1970-01-01
        • 2019-07-10
        • 1970-01-01
        • 2011-06-11
        • 2013-02-06
        相关资源
        最近更新 更多