laravel集合函数-pop(),shift(),prepend(),push()

169 阅读1分钟

shift()删除并返回集合中的第一个元素

**

$collection = collect([1, 2, 3, 4, 5]);
$first = $collection->shift();// 1

pop删除并返回集合中最后一个元素

**

$last = $collection->pop(); // 5
return $collection; // [2,3,4]

prepend() 将指定的项目 添加到 集合头部

**

$collection = collect([1,2,3,4,5]);
$collection->prepend(6); //[6,1,2,3,4,5] 
$collection->prepend(8, 'key'); //{"key":8,"0":1,"1":2,"2":3,"3":4,"4":5}%    

push() 将指定的值 添加到 集合末尾

**

$collection = collect([1,2,3,4,5]);
return $collection->push(8); // [1,2,3,4,5,8]

作者:xudong7930
链接:www.jianshu.com/p/0cef82c9a…
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。