How to skip the 1st key in an array loop?

Solution 1:

For reasonably small arrays, use array_slice to create a second one:

foreach(array_slice($_POST['info'],1) as $key=>$value)
{
    echo $value;
}

Solution 2:

foreach(array_slice($_POST['info'], 1) as $key=>$value) {
    echo $value;
}

Alternatively if you don't want to copy the array you could just do:

$isFirst = true;
foreach($_POST['info'] as $key=>$value) {
    if ($isFirst) {
        $isFirst = false;
        continue;
    }   
    echo $value;
}

Solution 3:

Couldn't you just unset the array...

So if I had an array where I didn't want the first instance, I could just:

unset($array[0]);

and that would remove the instance from the array.

Solution 4:

If you were working with a normal array, I'd say to use something like

foreach (array_slice($ome_array, 1) as $k => $v {...

but, since you're looking at a user request, you don't have any real guarantees on the order in which the arguments might be returned - some browser/proxy might change its behavior or you might simply decide to modify your form in the future. Either way, it's in your best interest to ignore the ordering of the array and treat POST values as an unordered hash map, leaving you with two options :

  • copy the array and unset the key you want to ignore
  • loop through the whole array and continue when seeing the key you wish to ignore