如何在 PHP 中的特定索引处启动 foreach 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3229905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start a foreach loop at a specific index in PHP
提问by Nialls Chavez
I am writing a foreachthat does not start at the 0th index but instead starts at the first index of my array. Is there any way to offset the loop's starting point?
我正在编写一个foreach不是从第 0 个索引开始而是从数组的第一个索引开始的。有没有办法抵消循环的起点?
回答by TRiG
回答by Gordon
A Foreachwill reset the array:
AForeach将重置数组:
Note: When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.
注意:当 foreach 第一次开始执行时,内部数组指针会自动重置为数组的第一个元素。这意味着您不需要在 foreach 循环之前调用 reset()。
Either use a forloop (only if this is notan associative array)
要么使用for循环(仅当这不是关联数组时)
$letters = range('a','z');
for($offset=1; $offset < count($letters); $offset++) {
echo $letters[$offset];
}
or a whileloop (can be any array)
或while循环(可以是任何数组)
$letters = range('a','z');
next($letters);
while($letter = each($letters)) {
echo $letter['value'];
}
or with a LimitIterator
$letters = new LimitIterator(new ArrayIterator(range('a','z')), 1);
foreach($letters as $letter) {
echo $letter;
}
which lets you specify start offset and count through the constructor.
它允许您通过构造函数指定起始偏移量和计数。
All of the above will output the letters b to z instead of a to z
以上所有将输出字母 b 到 z 而不是 a 到 z
回答by Cristian
You can use the array_slicefunction:
您可以使用该array_slice功能:
$arr = array(); // your array
foreach(array_slice($arr, 1) as $foo){
// do what ever you want here
}
Of course, you can use whatever offset value you want. In this case, 1'skip' the first element of the array.
当然,您可以使用任何您想要的偏移值。在这种情况下,1“跳过”数组的第一个元素。
回答by 2ndkauboy
In a foreach you cant do that. There are only two ways to get what you want:
在 foreach 中你不能这样做。只有两种方法可以得到你想要的东西:
- Use a for loop and start at position 1
- use a foreach and use a something like if($key>0) around your actual code
- 使用 for 循环并从位置 1 开始
- 使用 foreach 并在实际代码周围使用类似 if($key>0) 的东西
A foreach does what its name is telling you. Doing something for every element :)
foreach 做它的名字告诉你的事情。为每个元素做一些事情:)
EDIT: OK, a very evil solution came just to my mind. Try the following:
编辑:好的,我想到了一个非常邪恶的解决方案。请尝试以下操作:
foreach(array_reverse(array_pop(array_reverse($array))) as $key => $value){
...
}
That would reverse the array, pop out the last element and reverse it again. Than you'll have a element excluding the first one.
这将反转数组,弹出最后一个元素并再次反转它。比你有一个不包括第一个元素的元素。
But I would recommend to use one of the other solutions. The best would be the first one.
但我建议使用其他解决方案之一。最好的应该是第一个。
And a variation: You can use array_slice() for that:
还有一个变化:您可以使用 array_slice() :
foreach(array_slice($array, 1, null, true) as $key => $value){
...
}
But you should use all three parameters to keep the keys of the array for your foreach loop:
但是您应该使用所有三个参数来为您的 foreach 循环保留数组的键:
回答by Robert Swisher
Seems like a for loop would be the better way to go here, but if you think you MUST use foreach you could shift the first element off the array and unshift it back on:
似乎 for 循环将是更好的方法,但是如果您认为必须使用 foreach ,则可以将第一个元素移出数组并将其重新移回:
$a = array('foo','bar');
$temp = array_shift($a);
foreach ( $a as $k => $v ) {
//do something
}
array_unshift($a, $temp);
回答by Hernanibus
Well no body said it but if you don't mind altering the array and if we want to start from the second element of the given array:
好吧,没有人说过,但如果您不介意更改数组,并且我们想从给定数组的第二个元素开始:
unset($array[key($array)]);
foreach($array as $key=>$value)
{
//do whatever
}
if you do mind, just add,
如果你介意,只需添加,
$saved = $array;
unset($array[key($array)]);
foreach($array as $key=>$value)
{
//do whatever
}
$array = $saved;
Moreover if you want to skip a given known index, just subtitute
此外,如果您想跳过给定的已知索引,只需替换
key($array)
by the given index
通过给定的索引

