如何访问 Laravel 集合中的第 n 个项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40444759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access the nth item in a Laravel collection?
提问by D?uris
I guess I am breaking all the rules by deliberately making a duplicate question...
我想我故意提出重复的问题违反了所有规则......
The other questionhas 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:
让我们从头开始 - 该first()
方法的实现大致如下:
foreach ($collection as $item)
return $item;
It is obviously more robust than taking $collection[0]
or using other suggested methods.
它显然比采用$collection[0]
或使用其他建议的方法更可靠。
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:
即使集合中有 20 个项目,也可能没有带有索引0
或索引的15
项目。为了说明这个问题,让我们从文档中取出这个集合:
$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
?
现在,我们是否有任何可靠的(最好是简洁的)方式来访问第 n 项$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?
但这会在$keyed->last()
任何时候给出错误的项目 ( ) $n > $keyed->count()
。如果第 n 个项目存在并且null
它不只是first()
行为,我们如何获得第 n 个项目?
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->first()
。如何获得第二个?
$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(3)
应该返回'c'
(或者'c'
如果是基于 0 的,但这将与 不一致first()
)。$col[3]
不起作用,它只会返回错误。
$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'
.
$col->nth(7)
应该返回,null
因为没有第七项,只有四项。$col[7]
不起作用,它只会返回'd'
。
You could rephrase the question as "How to get nth item in the foreach order?" if it's more clear for some.
您可以将问题重新表述为“如何获取 foreach 订单中的第 n 个项目?” 如果对某些人来说更清楚。
回答by Alexey Mezenin
回答by Amit Gupta
回答by Muhammad Syauqy
Based on Alexey's answer, you can create a macro in AppServiceProvider (add it inside register method):
根据 Alexey 的回答,您可以在 AppServiceProvider 中创建一个宏(将其添加到 register 方法中):
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
回答by tylik
Maybe not the best option, but, you can get item from array inside collection
也许不是最好的选择,但是,您可以从集合内的数组中获取项目
$collection->all()[0]