4

Do the laravel collection methods (or PHP array methods, for that matter) have a way to shift off the first key/value pair of a collection?

That is, if I have the following small program

$collection = collect(['key1'=>'value1','key2'=>'value2']);        

var_dump(
    $collection->first()
);

var_dump(
    $collection->shift()
);

I can shift() value1 off the beginning of the collection, or grab it without removing it via first(). What I'd like is way, with one line of code, to shift off or grab the key of the first value (i.e. key1). I know I can do something like this

$result = (function($c){
    foreach($c as $key=>$value)
    {
        return $key;
    }
})($collection);

but I was hoping-that/wondering-if Laravel had something more elegant/compact.

2
  • Do you not care about the values at all? Commented Aug 7, 2017 at 18:46
  • @Devon I care about them some -- but if there's no way to grab both the value's just one line of additional code away with they key. Commented Aug 7, 2017 at 18:47

3 Answers 3

10

Grabbing an element (fist or last):

First one

$collection->take(1);

Result

=> Illuminate\Support\Collection {#926
     all: [
       "key1" => "value1",
     ],
   }

Last one

$collection->take(-1);

Result

=> Illuminate\Support\Collection {#924
     all: [
       "key2" => "value2",
     ],
   }

Grabbing first key I

$collection->take(1)->keys()->first();

Result

"key1"

Grabbing first key II

key($collection->take(1)->toArray());

Result

"key1"
3

One way to grab the keys is using

$collectionOfKeys = $collection->keys();

To get the first key, you could do:

$key = $collection->keys()->first();

However, since keys() will return a new collection, it's not possible to shift the values off of the original collection in one line, but you could use forget with the $key or just shift the original collection afterwards.

$collection->forget($key);
// or $collection->shift();
4
  • That's what I would do as well. It's clean, self-explanatory and uses dedicated Laravel methods.
    – lesssugar
    Commented Aug 7, 2017 at 19:37
  • @lesssugar you would ask Laravel to build an array of keys with potentially millions of elements just because you need a first element? Why?
    – Axalix
    Commented Aug 7, 2017 at 19:53
  • I would agree that splice() is the most efficient way of handling this, I was putting up an example when Jean posted his answer. This way works, but on a large collection would be inefficient. Commented Aug 7, 2017 at 19:56
  • @Axalix You got me.
    – lesssugar
    Commented Aug 7, 2017 at 20:20
3

Splice can work for you:

$collection = collect(['key1'=>'value1','key2'=>'value2']); 

$chunked = $collection->splice(0, 1);

print_r($chunked);
//Array([key1] => value1)

print_r($collection->all());
//Array ( [key2] => value2 )

See: https://laravel.com/docs/5.4/collections#method-splice

1
  • Beat me to it, was just updating my answer. Commented Aug 7, 2017 at 18:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.