Bring the news cycle `How can I skip the first entry and not output?

  • add a boolean variable flag - JVic
  • five
    better solve this issue at the stage of data preparation, when sampling from the database ( limit-offset ), or in the controller (in logic ( array_pop )), and not at the final stage in the view. - teran

1 answer 1

Option 1. Use for instead of foreach and start walking through the collection with the first element.

  for (int $i = 1; $i < length; $i++) { //TODO } 

Option 2. Create a boolean variable and check if it has changed.

  $flag = true; foreach ($collection as $value) { if ($flag) { $flag = false; } else { //TODO } } 

Option 3. Remove the first element from the array before passing through it.

 unset($array[0]); 

Option 4. In general, do not fill out the collection with this element at the stage of forming a query to the database.

  • 3
    in the for loop, it's easier to start with 1 without any if , don't you think? - Alexey Shimansky
  • @ Alexey Shimansky, of course, easier :) Corrected - koks_rs