Hi,

I am using an array with 10 elements in it. I want to be able to loop through the array a number of times, but am unsure if you can start looping trough an array at a given element.

At the moment I use:

foreach($filelist as $row);{
echo "$row\n";
}

and this lists all 10 elements in the array.

I have worked out how to stop the loop when I fine the element I want.

So my question is, can you iterate through the array, but start at element 5, instead of 0?

Cheers

    You could use a for loop (so long as it's not an associative array) like so

    <?php
    for($i=5;$i<count($array);$i++) {
      echo($array[$i]."<br />\n");
    }
    ?>

    if it is an associative array you can set a condition to skip by in a for loop but it will still have to cycle through the first elements

    <?php
    for($i=0;$data=each($array);$i++) {
      if($i<5) { continue; }
      echo($data."<br />\n");
    }
    ?>

    HTH
    Bubble

      Thanks bubblenut, that made it a whole lot clearer and works well.

      It seems so simple 🙂 I was in a real brain fog trying to work it out.

      Thanks again for your help.

        Write a Reply...