如上面的代码示例,Collection 类支持链式调用,一般来说,每一个 Collection 方法会返回一个全新的 Collection 实例,你可以放心地进行链接调用。创建集合如上所述,collect 辅助函数会利用传入的数组生成一个新的 Illuminate\Support\Collection 实例。所以要创建一个集合就这么简单:$collection = collect([1, 2, 3]...
该unique 方法返回集合中所有唯一项。返回的集合保留着原数组的键,所以在这个例子中,我们使用 values 方法把键重置为连续编号的索引:$collection = collect([1, 1, 2, 2, 3, 4, 2]); $unique = $collection->unique(); $unique->values()->all(); // [1, 2, 3, 4]...
$collection = collect([ ['name' => 'Sally'], ['school' => 'Arkansas'], ['age' => 28] ]); $flattened = $collection->flatMap(function ($values) { return array_map('strtoupper', $values); }); $flattened->all(); // ['name' => 'SALLY', 'school' => 'ARKANSAS', 'age' ...
$collection->contains('product', 'Bookcase'); // falseThe contains method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an integer of the same value. Use the containsStrict method to filter using "strict" comparisons.For...
Since collect() is part of the Enumerable contract, you can safely use it to get a Collection instance.combine()The combine method combines the values of the collection, as keys, with the values of another array or collection:$collection = collect(['name', 'age']);$combined = $...
1$collection = collect([1, 2, 3, 4, 5]); 2 3$collection->contains(function ($key, $value) { 4 return $value > 5; 5}); 6 7// falseThe contains method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an...
]);$flattened=$collection->flatMap(function($values) {returnarray_map('strtoupper',$values); });$flattened->all();//['name' => 'SALLY', 'school' => 'ARKANSAS', 'age' => '28'];#18.flatten方法,将多维集合转为一维。$collection= collect(['name' => 'taylor', 'languages' => ['...
unique uniqueStrict where whereStrict whereIn whereInStrict whereNotIn whereNotInStrict 改变 flatten 方法将多维集合转为一维的 flip 方法将集合中的键和对应的数值进行互换 groupBy 方法根据给定的键对集合内的项目进行分组 keyBy 方法以给定的键作为集合的键。如果多个项目具有相同的键,则只有最后一个项目会显示...
$collection = collect(['pinux', 'php', null])->map(function ($name) { return strtoupper($name); }) ->reject(function ($name) { return empty($name); }); 1. 2. 3. 4. 5. 6. 上面的列子可以看出,Collection 类允许你链式调用其方法,以达到在底层数组上优雅地执行 ...
('id')->values()->unique()->all();$models=$model->getScoutModelsByIds($builder,$keys)->keyBy($model->getKeyName());returnCollection::make($results)->map(function($hit)use($model,$models){$key=$hit['id'];if(isset($models[$key])){return$models[$key];}returnnull;})->filter(...