12

我有关联数组,例如:

$myArray = array(
  'key1' => 'val 1',
  'key2' => 'val 2'
  ...
);

我不知道前面的键值,但想从第二个元素开始循环。在上面的示例中,这将是从 key2 开始。

我试过了

foreach(next(myArray) as $el) {

}

但这没有用。

替代方案可能是array_slice,但这似乎很混乱。我错过了一些明显的东西吗?

4

8 回答 8

25

There really is no "one true way" of doing this. So I'll take it as a benchmark as to where you should go.

All information is based on this array.

$array = array(
    1 => 'First',
    2 => 'Second',
    3 => 'Third',
    4 => 'Fourth',
    5 => 'Fifth'
);

The array_slice() option. You said you thought this option was overkill, but it seems to me to be the shortest on code.

foreach (array_slice($array, 1) as $key => $val)
{
    echo $key . ' ' . $val . PHP_EOL;
}

Doing this 1000 times takes 0.015888 seconds.


There is the array functions that handle the pointer, such as ...

  • current() - Return the current element in an array.
  • end() - Set the internal pointer of an array to its last element.
  • prev() - Rewind the internal array pointer.
  • reset() - Set the internal pointer of an array to its first element.
  • each() - Return the current key and value pair from an array and advance the array cursor.

Please note that the each() function has been deprecated as of PHP 7.2, and will be removed in PHP 8.

These functions give you the fastest solution possible, over 1000 iterations.

reset($array);
while (next($array) !== FALSE)
{
    echo key($array) . ' ' . current($array) . PHP_EOL;
}

Doing this 1000 times, takes 0.014807 seconds.


Set a variable option.

$first = FALSE;
foreach ($array as $key => $val)
{
    if ($first != TRUE)
    {
        $first = TRUE;
        continue;
    }

    echo $key . ' ' . $val . PHP_EOL;
}

Doing this 1000 times takes 0.01635 seconds.


I rejected the array_shift options because it edits your array and you've never said that was acceptable.

于 2013-09-08T21:26:10.027 回答
5

这取决于您是否只想执行一次或多次,以及您以后是否仍需要原始数组。

“第一”模式

$first = true;
foreach ($array as $key=>value) {
    if($first) {
        $first = false;
        continue;
    }
    // ... more code ...
}

我个人经常使用这个解决方案,因为它真的很简单,每个人都明白这一点。此外,创建新数组不会影响性能,并且您仍然可以在循环后对原始数组进行操作。

但是,如果你有几个这样的循环,它开始看起来有点不干净,因为每个循环需要 5 行额外的代码。

数组移位

array_shift($array);
foreach ($array as $key=>value) {
    // .... more code ....
}

array_shift是针对这种不需要第一个元素的特殊情况量身定制的功能。从本质上讲,这是一种 Perl 风格的说法$array = array_slice($array, 1),可能并不完全明显,尤其是因为它修改了原始数组。

因此,如果您需要多次移位的数组以及以后的原始数组,您可能想要制作原始数组的副本并将其移位。

数组切片

当然,还有它array_slice自己。array_slice如果您希望原始数组保持不变并且您需要多次切片数组,我认为没有任何问题。然而,如果你确定你总是想只切掉一个元素,你不妨使用速记array_shift(如果需要,在之前制作一个副本)。

于 2013-09-08T20:46:07.593 回答
2

灵活迭代的另一种方式:

reset($myArray);  // set array pointer to the first element  
next($myArray);   // skip first element

while (key($myArray) !== null) {

  // do something with current($myArray)

  next($myArray);
}

据我所知foreach,这只是这种构造的一种捷径。

来自 Zend PHP 5 认证学习指南:

如您所见,使用这组函数 [reset, next, key, current, ...] 需要相当多的工作;公平地说,在某些情况下,它们提供了唯一合理的遍历数组的方法,特别是当您需要在其元素之间来回跳过时。但是,如果您需要做的只是从头到尾遍历整个数组,PHP 以 foreach() 构造的形式提供了一个方便的快捷方式。

于 2013-09-08T20:22:55.443 回答
2

您可以采用明显的方式:

$flag = false;
foreach($myArray as $el) {
   if($flag) {
      // do what you want
   }
   $flag = true;
}
于 2013-09-08T20:14:13.923 回答
2

如果您的数组是基于 0 的,那么它将是if($key>=1),但是当您的数组从键 1开始时,这应该可以工作。

foreach ($array as $key=>$value){if($key>=2){
        do stuff    
        }}
于 2015-04-17T05:59:46.063 回答
1

你可以试试:

$temp = array_shift($arr);
foreach($arr as $val) {
   // do something
}
array_unshift($arr, $temp);
于 2013-09-08T20:18:20.150 回答
1
reset($myArray);  
next($myArray); 

while ($element = each($myArray))
{
    //$element['key'] and $element['value'] can be used
}
于 2013-09-08T20:26:48.470 回答
0

出现此问题时,我的解决方案相当简单。它有一个很好的优势,即很容易修改,如果你愿意的话,可以跳过第一个元素。

$doomcounter = 0;    
foreach ($doomsdayDevice as $timer){ if($doomcounter == 0){$doomcounter++; continue;} 

   // fun code goes here

}
于 2015-01-29T19:19:16.883 回答