Laravel Collection
whereIn 删选数据
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newCollection = $colletion->whereIn("orgId", [2,3]);
dd($newCollection);
where/whereStrict 删选数据
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newCollection = $colletion->where("orgId", 1);
dd($newCollection);
toJson 集合转JSON 基本上来说不需要
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
dd($colletion->toJson());
对某一项取和
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
dd($colletion->sum("orgId"));
集合分页取数据 splice
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
dd($colletion->splice(1,10));
sort集合排序 sortBy/sortByDesc
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newCollection = $colletion->sortByDesc("icon");
dd($newCollection);
search 搜索某一项在集合中是否存在
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newCollection = $colletion->search(function($item){
return $item["orgId"] == 4;
});
dd($newCollection);
集合数据反转 reverse
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newCollection = $colletion->reverse();
dd($newCollection);
在集合中添加一条数据 push
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newCollection = $colletion->push([
"orgId" => 4,
"name" => "心动",
"icon" => "icon4"
]);
dd($newCollection);
pull 方法通过键从集合中移除并返回数据项
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newCollection = $colletion->keyBy(function($item){
return $item["orgId"];
});
$ttCollection = $newCollection->pull("1"); 返回删除项
dd($newCollection); 原集合已经改变
prepend 添加数据到集合
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newCollection = $colletion->prepend([
"orgId" => 4,
"name" => "哈哈",
"icon" => "icon4"
]);
dd($newCollection);
pluck 方法为给定键获取所有集合值:
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newCollection = $colletion->pluck("name");
dd($newCollection);
返回某一项的最大值 max
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
dd($colletion->max("orgId"));
mapwithkeys 可以用来返回某些列的数据 mapWithKeys
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newCollection = $colletion->mapWithKeys(function($item){
return [["orgId" => $item["orgId"], "name" => $item["name"]]];
});
dd($newCollection);
修改集合 map
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newCollection = $colletion->map(function($item){
$item["icon"] = "hehe";
return $item;
});
var_dump($newCollection);
取出嵌套集合的key keyBy
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$orgCollection = $colletion->keyBy(function($item){
return $item["orgId"];
});
var_dump($orgCollection->keys());
为嵌套集合生成Key keyBy
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$orgCollection = $colletion->keyBy(function($item){
return $item["orgId"];
});
var_dump($orgCollection);
数组是否为空 isEmpty
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
var_dump($colletion->isEmpty());
获取某一列的数据,返回的是字符串 implode
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newString = $colletion->implode("name", ",");
var_dump(json_decode($newString));
数据分页 forPage
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newCollection = $colletion->forPage(1,2);
var_dump($newCollection);
获取集合的第一个元素, 可以根据条件来删选 first
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newCollection = $colletion->first(function($item){
return $item["orgId"]>2;
});
var_dump($newCollection);
过滤条件, 删选数据 filter
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$newCollection = $colletion->filter(function($item) {
return $item["orgId"] == 2;
});
var_dump($newCollection);
迭代每一个数据项 each
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
$colletion->each(function($item, $key){
if ($item["orgId"] == 1) { 暂停迭代
return false;
}
});
var_dump($colletion);
集合长度 count
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
var_dump(count($colletion));
是否包含某个值 contains
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
var_dump($colletion->contains(["orgId"], 4));
返回某一项的平均值 avg
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
echo $colletion->avg("orgId");
集合删选 reject
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
var_dump($colletion);
$newCollection = $colletion->reject(function($item){
return $item["orgId"] == 1;
}) ;
var_dump($newCollection);
var_dump($colletion);
集合转数组 toArray
$colletion = collect([[
"orgId" => 1,
"name" => "公司名",
"icon" => "icon1"
],[
"orgId" => 2,
"name" => "公司名",
"icon" => "icon2"
],[
"orgId" => 3,
"name" => "公司名",
"icon" => "icon3"
]]);
var_dump($colletion->toArray());
var_dump($colletion->all());
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: thinkphp5.x之Collection(集合)解析 php集合
- 下一篇: EL表达式