iamnards
iamnards

Reputation: 217

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

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 :)

Upvotes: 6

Views: 7253

Answers (8)

Spudley
Spudley

Reputation: 168755

Not directly with a foreach(). The clue is in the each part of the name. It loops through all elements.

So how can we achieve it?

You could always just have an if() clause inside the loop that checks the key value before doing the rest of the work.

But if that's not workable for you, for whatever reason, I'd suggest using a FilterIterator to achieve this.

The FilterIterator is a standard PHP class which you can extend to create your own filters. The iterator can then be looped using a standard foreach() loop, picking up only the records that are accepted by the filter.

There are some examples on the manual page linked above that will help, but here's a quick example I've thrown together for you:

class SkipTopFilter extends FilterIterator {
    private $filterNum = 0;
    public function __construct(array $array, $filter) {
        parent::__construct(new ArrayIterator($array));
        $this->filterNum = $filter;
    }
    public function accept() {
        return ($this->getInnerIterator()->key() >= $this->filterNum);
    }
}

$myArray = array(13,6,8,3,22,88,12,656,78,188,99);
foreach(new SkipTopFilter($myArray, 6) as $key=>$value) {
    //loop through all records except top six.
    print "rec: $key => $value<br />";
}

Tested; outputs:

rec: 6 => 12
rec: 7 => 656
rec: 8 => 78
rec: 9 => 188
rec: 10 => 99

Upvotes: 6

mpyw
mpyw

Reputation: 5754

$i = 1;
foreach ($temp_row as $key => $value) {
    if (($num = $i++) < 6) continue;
    // Do something
}

Or

for ($i = 0; $i < 5; $i++) next($temp_row);
while(list($key, $value) = each($temp_row)) {
    // Do something 
}

Upvotes: 0

Alex Tselegidis
Alex Tselegidis

Reputation: 155

try this code:

$new_temp_row = array_slice($temp_row, 5);

foreach($new_temp_row as $key => $value) {
    echo $value;
}

Upvotes: 1

hpoom
hpoom

Reputation: 53

I suggest you use a for look instead of a for each. You can then access both the key and value that are at i position inside your loop.

<?php
$num = 1;
$keys = array_keys( $temp_row );
for( $i = 5; $i < count( $temp_row ); $i++ ) {
            $key = $keys[$i];
            $val = $temp_row[$i];
    echo $i;
}
?>

Upvotes: 0

Eugene
Eugene

Reputation: 1959

You may create new array with needed data part of origin array and work with it:

$process = array_slice ($temp_row, 5);
foreach($process as $key => $value) {

    //TODO Your logic
}

Or you may skipp first siel elments:

<?php
$num=1;
foreach($temp_row as $key => $value) {
    if ($num < 6) {
         $num++;
         continue;
    }
    //TODO Your logic
}
?>

Upvotes: 0

Wim Molenberghs
Wim Molenberghs

Reputation: 902

Or with a for loop

$num = 1;
for($i=5; $i<= count($temp_row), $i++) {
   echo $num;
   $num++;
}

Upvotes: 2

Robert
Robert

Reputation: 20286

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

Upvotes: 17

Ian Wood
Ian Wood

Reputation: 6573

you could skip if counter is not 6th element...

<?php
$num=0;
foreach($temp_row as $key => $value) {
    if( ++$num < 6 )
    {
        continue;
    }
    echo $num;
}
?>

Upvotes: 2

Related Questions