Start foreach loop on associative array in second element

I have an associative array, for example:

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

I do not know the key value in front, but I want to start the loop from the second element. In the above example, this will be from key2 forward.

I tried

 foreach(next(myArray) as $el) { } 

but it didn’t work.

Alternatives may be array_slice , but that seems messy. Am I missing something obvious?

+8
php
source share
8 answers

There is really no “one true way” of this. Therefore, I will take it as a guide 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' ); 

Array_slice () parameter. You said that you think this option is too crowded, but it seems to me that it is the shortest in code.

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

Doing this 1000 times takes 0.015888 seconds.


There are array functions that handle a pointer, for example ...

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

Note that each() function is deprecated from PHP 7.2 and will be removed in PHP 8.

These features give you the fastest solution, over 1000 iterations.

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

Performing this 1000 times takes 0.014807 seconds.


Set the 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 parameters because it edits your array and you never said it was acceptable.

+17
source share

It depends on whether you want to do this one or more times, and also on whether you need another source array later.

"First" template

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

I personally use this solution quite often, because it is really straightforward, everyone gets it. In addition, there is no performance improvement when creating a new array, and you can continue to work with the original array after the loop.

However, if you have several loops like this, it starts to look a little unclean because you need 5 extra lines of code per loop.

array_shift

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

array_shift is a function adapted to this special case, not requiring the first element. Essentially, this is a Perl-ish way of saying $array = array_slice($array, 1) , which may not be entirely explicit, especially since it modifies the original array.

So, you can make a copy of the original array and move it if you need both the shifted array several times and the original array later.

array_slice

And of course there is array_slice . I see nothing wrong with array_slice if you want the original array to remain unchanged and you need a sliced ​​array several times. However, if you are sure that you always want to cut only one element, you can also use the shortened array_shift (and make a copy before that if necessary).

+4
source share

Another way to flexibly iterate:

 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); } 

As far as I know, foreach is just a kind of shortcut for this design.

From the Zend PHP 5 Certification tutorial:

As you can see, using this set of functions [reset, next, key, current, ...] requires a lot of work; to be fair, there are some situations where they offer the only reasonable way to iterate through an array, especially if you need to skip back and forth between its elements. If, however, all you have to do is repeat through the entire array from start to finish, PHP provides a convenient shortcut in the form of a foreach () construct.

+2
source share

If your array was based on 0, that would be if($key>=1) , but as your array starts with key 1, this should work.

 foreach ($array as $key=>$value){if($key>=2){ do stuff }} 
+2
source share

You can go in the obvious way:

 $flag = false; foreach($myArray as $el) { if($flag) { // do what you want } $flag = true; } 
+1
source share

You can try:

 $temp = array_shift($arr); foreach($arr as $val) { // do something } array_unshift($arr, $temp); 
+1
source share
 reset($myArray); next($myArray); while ($element = each($myArray)) { //$element['key'] and $element['value'] can be used } 
+1
source share

My pretty simple solution when this problem occurs. It has a nice advantage because it is easy to modify to be able to skip more than just the first element if you want it.

 $doomcounter = 0; foreach ($doomsdayDevice as $timer){ if($doomcounter == 0){$doomcounter++; continue;} // fun code goes here } 
0
source share

All Articles