Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: how to do foreach and start from sixth element of collection?

Tags:

loops

foreach

php

is it possible to start a foreach loop from specific element [sixth element]?

im using this code:

    <?php
    $num=1;
    foreach($temp_row as $key => $value) {

        echo $num;

        $num++;
    }
    ?>

thanks :)

like image 516
iamnards Avatar asked May 24 '13 09:05

iamnards


People also ask

How do I start a foreach key in 1 PHP?

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

Can I use continue in foreach PHP?

Introduction. The continue statement is one of the looping control keywords in PHP. When program flow comes across continue inside a loop, rest of the statements in current iteration of loop are skipped and next iteration of loop starts. It can appear inside while, do while, for as well as foreach loop.


1 Answers

You can use for example array_slice()

$num = 5; //it will start from sixth element

foreach(array_slice($temp_row, $num) as $key => $value) {

     echo $key.'=>'.$value.'<br>';
}
like image 139
Robert Avatar answered Oct 03 '22 22:10

Robert