[Solved] How can I start looping with item of index 1 from a php array?


Instead of the standard for loop to go through an array, for example:

for($i = 0; $i < count($array); $i++) {
    echo $array[$i] . " ";
}

Outputs “one two three four five”.

Just set $i equal to 1 instead, like so:

for($i = 1; $i < count($array); $i++) {
    echo $array[$i] . " ";
}

Now it outputs this: “two three four five”.

However, if you wanted to get the last four items of an array, you can use array_slice().

$sliced = array_slice($array, -4);

for($i = 0; $i < count($sliced); $i++) {
    echo $sliced[$i] . " ";
}

Outputs “two three four five”.

solved How can I start looping with item of index 1 from a php array?