How to start foreach loop with a specific index in PHP

I am writing a foreach that does not start at the 0th index, but instead starts at the first index of my array. Is there a way to compensate for the starting point of the loop?

+42
php
Jul 12 '10 at 15:48
source share
6 answers

Keep it simple.

 foreach ($arr as $k => $v) { if ($k < 1) continue; // your code here. } 
+66
Jul 12 '10 at 16:00
source share
— -

A Foreach will reset the array:

Note. When foreach first starts, the internal array pointer will automatically reset to the first element of the array. This means that you do not need to call reset () before the foreach loop.

Or use a for loop (only if it's not an associative array)

 $letters = range('a','z'); for($offset=1; $offset < count($letters); $offset++) { echo $letters[$offset]; } 

or a while (can be any array)

 $letters = range('a','z'); next($letters); while($letter = each($letters)) { echo $letter['value']; } 

or with LimitIterator

 $letters = new LimitIterator(new ArrayIterator(range('a','z')), 1); foreach($letters as $letter) { echo $letter; } 

which allows you to specify the initial offset and calculate through the constructor.

All of the above will print the letters b in z instead of a in z

+27
Jul 12 2018-10-12T00:
source share

You can use the array_slice function:

 $arr = array(); // your array foreach(array_slice($arr, 1) as $foo){ // do what ever you want here } 

Of course, you can use any offset value you want. In this case, 1 "skip" the first element of the array.

+11
Jul 12 '10 at 15:53
source share

In anticipation of this, you cannot do this. There are only two ways to get what you want:

  • Use the for loop and start at position 1
  • use foreach and use something like if ($ key> 0) around your actual code.

A foreach does what his name tells you. Doing something for each element :)

EDIT : OK, a very bad decision came to my mind. Try the following:

 foreach(array_reverse(array_pop(array_reverse($array))) as $key => $value){ ... } 

This will change the array, pull out the last element and undo it again. Then you will have an element that excludes the first.

But I would recommend using one of the other solutions. Best will be first.

And option: you can use array_slice () for this:

 foreach(array_slice($array, 1, null, true) as $key => $value){ ... } 

But you must use all three parameters to save the array keys for your foreach loop:

+2
Jul 12 '10 at 15:52
source share

There seems to be a better way for the loop, but if you think you SHOULD use foreach, you can move the first element from the array and disconnect it again:

 $a = array('foo','bar'); $temp = array_shift($a); foreach ( $a as $k => $v ) { //do something } array_unshift($a, $temp); 
+1
Jul 12 2018-10-12T00:
source share

Well, no one said this, but if you do not mind changing the array, and if we want to start with the second element of this array:

 unset($array[key($array)]); foreach($array as $key=>$value) { //do whatever } 

if you don't mind, just add

 $saved = $array; unset($array[key($array)]); foreach($array as $key=>$value) { //do whatever } $array = $saved; 

Also, if you want to skip this known index, just substitute

 key($array) 

on this index

0
Apr 21 '16 at 8:13
source share



All Articles