How to access the nth item in a Laravel collection?
Asked Answered
P

5

31

I guess I am breaking all the rules by deliberately making a duplicate question...

The other question has an accepted answer. It obviously solved the askers problem, but it did not answer the title question.

Let's start from the beginning - the first() method is implemented approximately like this:

foreach ($collection as $item)
    return $item;

It is obviously more robust than taking $collection[0] or using other suggested methods.

There might be no item with index 0 or index 15 even if there are 20 items in the collection. To illustrate the problem, let's take this collection out of the docs:

$collection = collect([
    ['product_id' => 'prod-100', 'name' => 'desk'],
    ['product_id' => 'prod-200', 'name' => 'chair'],
]);

$keyed = $collection->keyBy('product_id');

Now, do we have any reliable (and preferably concise) way to access nth item of $keyed?

My own suggestion would be to do:

$nth = $keyed->take($n)->last();

But this will give the wrong item ($keyed->last()) whenever $n > $keyed->count(). How can we get the nth item if it exists and null if it doesn't just like first() behaves?

Edit

To clarify, let's consider this collection:

$col = collect([
    2 => 'a',
    5 => 'b',
    6 => 'c',
    7 => 'd']);

First item is $col->first(). How to get the second?

$col->nth(3) should return 'c' (or 'c' if 0-based, but that would be inconsistent with first()). $col[3] wouldn't work, it would just return an error.

$col->nth(7) should return null because there is no seventh item, there are only four of them. $col[7] wouldn't work, it would just return 'd'.

You could rephrase the question as "How to get nth item in the foreach order?" if it's more clear for some.

Psoriasis answered 6/11, 2016 at 0:23 Comment(2)
As in the answer to the question you mentioned, you can do $collection->get($nth, 'some default value')Savina
@tam It uses the keys just like [$nth] does...Strickman
B
35

I guess faster and more memory-efficient way is to use slice() method:

$collection->slice($n, 1);
Babble answered 6/11, 2016 at 3:35 Comment(4)
This seems nice, but I am not sure about the behaviour if $n > $collection->count(). It appears that slice() uses PHPs array_slice() and I can't find info on behaviour if $n exceeds the size of array. Maybe it's unspecified and unreliable :|Strickman
It will just return an empty collection. You even can do easy check with ->isEmpty() or empty($result).Babble
Mkay, then $collection->slice($n, 1)->fiirst() will return nth item if it exists and null otherwise. Seems great :)Strickman
Two issues with this answer to consider: a) this returns a collection of one, so $collection->slice($n, 1)->first() must be used; b) $n is zero indexed, so will return the $n+1 element from the collection. So for this example use: $collection->slice($n-1, 1)->first()Lilybel
L
11

You can try it using values() function as:

$collection->values()->get($n);
Luau answered 6/11, 2016 at 5:5 Comment(2)
This seems the best solution so far, can't think of an example where it fails :)Strickman
It's working solution but it will duplicate whole collection and may kill an app, if collection is big by itself (collection of articles with relations or something similar).Babble
V
9

Based on Alexey's answer, you can create a macro in AppServiceProvider (add it inside register method):

use Illuminate\Support\Collection;

Collection::macro('getNth', function ($n) {
   return $this->slice($n, 1)->first();
});

and then, you can use this throughout your application:

$collection = ['apple', 'orange'];

$collection->getNth(0) // returns 'apple'
$collection->getNth(1) // returns 'orange'
$collection->getNth(2) // returns null
$collection->getNth(3) // returns null
Vernita answered 3/5, 2019 at 10:32 Comment(0)
G
9

you may use offsetGet since Collection class implements ArrayAccess

$lines->offsetGet($nth);
Guanine answered 6/8, 2021 at 10:45 Comment(0)
E
1

Maybe not the best option, but, you can get item from array inside collection

$collection->all()[0] 
Enlarger answered 9/11, 2018 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.