Jul*_*oro 3 collections laravel
我有一个这样的集合:
Collection {#750 ?
#items: array:18 [?
18 => User {#691 ?}
19 => User {#696 ?}
20 => User {#701 ?}
]
}
Run Code Online (Sandbox Code Playgroud)
18、19、20 应该不同的地方
我试着打电话
$collection->get(0);
$collection->get(1);
$collection->get(2);
Run Code Online (Sandbox Code Playgroud)
但显然,它不起作用
我找到了一种使用 shift 的解决方法,它返回第一个元素并将其从集合中删除,
$el1 = $collection->shift();
$el2 = $collection->shift();
$el3 = $collection->shift();
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我的原始收藏被破坏了。
知道我应该怎么做吗?
您可以使用slice()方法:
$collection->slice(0, 3)
Run Code Online (Sandbox Code Playgroud)
slice 方法返回从给定索引开始的集合切片。如果您想限制返回切片的大小,请将所需大小作为第二个参数传递。
您可以用作values():
$collection = $collection->values();
Run Code Online (Sandbox Code Playgroud)
那么你可以将它用作:
$collection->get(0);
$collection->get(1);
$collection->get(2);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4931 次 |
| 最近记录: |