Support the ongoing development of Laravel.io →
Requests Database Blade
Last updated 2 years ago.
0

Well there are a few ways. Do you need to keep the array/collection intact?

Last updated 2 years ago.
0

Nothing is set in stone, just what ever works the easiest works for me :)

If I had to remove [0] from the array do what ever and use the altered array for the remainder that would work.

Last updated 2 years ago.
0

You could do something like remove the first element from the array, do what you must, then loop through the remainder.

Or you can just loop through the whole collection/array and just do a check to see if it is the first element or not.

If your using a collection the first method could be like so

$first = $collection->shift();
// do what you need with the first element
foreach($collection as $item) { // loop through the rest }

shift() will get and remove the first item from the collection.

Last updated 2 years ago.
0

Or you can do this, which is the approach I use.

$first = true;

foreach($items as $item)
{
    if($first)
    {
        //modify first element
        //then set first to false
        $first = false;
    }
    else
    {
        //modify everything else
    }
}
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.