camillebaronnet/collection 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

camillebaronnet/collection

最新稳定版本:v0.4.0

Composer 安装命令:

composer require camillebaronnet/collection

包简介

无描述信息

README 文档

README

Collection provides a powerful data manipulation system based on the map/reduce/filter principles. Thanks to the php generators and the immutability, it allows to calculate the data as late as possible while preserving the memory.

Installation

composer require camillebaronnet/collection

How to use

public Collection::__construct(...$elements) public Collection::__construct(\Traversable|array $elements)

Exemple :

$collection = (new Collection('foo', 'bar')) ->map(fn($word) => 'hello '.$word) ->map(fn($word) => strtoupper($word)) ; // The data is computed only here var_dump($collection->toArray()); 
array(2) { [0]=> string(9) "HELLO FOO" [1]=> string(9) "HELLO BAR" } 

Basic operations

Note : As long as you use map/filter operations, nothing will be executed or stored in memory. If you decide to do more than one reduce or iterate operation, use get() first to before to buffer the previous operations.

Map

The map() function iterates over each element and applies the transformation function provided by the user.

map(fn($element) => /* ... */);

Example :

(new Collection(10, 50, 100)) ->map(fn($element) => $element * 2) ->toArray() ; // [20, 100, 200]

Filter

The filter() method test elements using that pass the test implemented by the provided function.

filter(fn ($element) => /* ... */);

Example :

(new Collection(10, 50, 100)) ->filter(fn($element) => $element > 20) ->toArray() ; // [50, 100]

Reduce

The reduce() method executes a reduction function provided by the user on each element, the result of each iteration is passed to the next iteration.

reduce(fn($carry, $currentValue, $currentKey) => /* ... */, $initial);

Example :

(new Collection(10, 50, 100)) ->reduce(fn($carry, $current) => $carry + $current) ; // 160

GroupBy

The method groupBy() returns a Collection of CollectionGroup. A CollectionGroup is a simple collection with a key property added.

groupBy(fn ($element) => /* ... */);

Example :

$result = (new Collection([ ['key1' => 'foo', 'key2' => 10], ['key1' => 'bar', 'key2' => 11], ['key1' => 'foo', 'key2' => 12], ['key1' => 'bar', 'key2' => 14], ])) ->groupBy(fn($x) => $x['key1']) ->toArray() // [CollectionGroup, CollectionGroup] ; $result[0]->key; // 'foo' $result[0]->toArray(); // [['key1' => 'foo', 'key2' => 10],['key1' => 'foo', 'key2' => 12],]

统计信息

  • 总下载量: 2.15k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: mit
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固