How to start a foreach loop at a specific index in PHP

Php

Php Problem Overview


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 any way to offset the loop's starting point?

Php Solutions


Solution 1 - Php

Keep it simple.

foreach ($arr as $k => $v) {
   if ($k < 1) continue;
   // your code here.
}

See the continue control structure in the manual.

Solution 2 - Php

A Foreach will reset the array:

> 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.

Either use a for loop (only if this is not an associative array)

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

or a while loop (can be any array)

$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

Solution 3 - Php

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 whatever offset value you want. In this case, 1 'skip' the first element of the array.

Solution 4 - Php

In a foreach you cant do that. There are only two ways to get what you want:

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

A foreach does what its name is telling you. Doing something for every element :)

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:

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:

Solution 5 - Php

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:

$a = array('foo','bar');
$temp = array_shift($a);
foreach ( $a as $k => $v ) {
  //do something
}
array_unshift($a, $temp);

Solution 6 - Php

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

Solution 7 - Php

bismillah... its simple just make own keys

 $keys=1;
    foreach ($namafile_doc as $value ) {
        $data['doc'.$keys]=$value;
        $keys++;
    }

may this answer can make usefull

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNialls ChavezView Question on Stackoverflow
Solution 1 - PhpTRiGView Answer on Stackoverflow
Solution 2 - PhpGordonView Answer on Stackoverflow
Solution 3 - PhpCristianView Answer on Stackoverflow
Solution 4 - Php2ndkauboyView Answer on Stackoverflow
Solution 5 - PhpRobert SwisherView Answer on Stackoverflow
Solution 6 - PhpHernanibusView Answer on Stackoverflow
Solution 7 - PhpSumitro Hadi HartoView Answer on Stackoverflow