Omitting an array member

Imagine that you have to pass an array from a foreach loop but omitting the first array member.

How can I do it?
One thought I made was to remove the array member before the loop and add it again after the loop ends.

What do you propose?

I’d go for array_slice() since it doesn’t modify the original array.

1 Like

Not quite sure I understand the question, but would something like this do it?

$flag = false
foreach ($array as $ix => $row) { 
  if ($flag == false) {
    $flag = true;
    }
  else {
    // do whatever you want with $ix and $row
  }
}

so it doesn’t do the action the first time through.

That’s possible, but it needs to execute the if condition for each item.

thiis seems to be OK…I will try it.

If it’s always and ever only the first, maybe you could “shift” it off the array?

http://php.net/manual/en/function.array-shift.php

That was @designtrooper’s first idea, although to work properly (more in terms of best practices) that implies that the foreach() is called in another function (or on a copy of the array), so the original array doesn’t get modified.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.