PHPackages                             wj/array-collection - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. wj/array-collection

ActiveLibrary

wj/array-collection
===================

turn array action to class function ,make it simple to use and to check

1.0.1(9y ago)012MITPHP

Since Feb 21Pushed 9y ago1 watchersCompare

[ Source](https://github.com/wangjia5693/array-to-collection)[ Packagist](https://packagist.org/packages/wj/array-collection)[ RSS](/packages/wj-array-collection/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

清晰操作array
=========

[](#清晰操作array)

通过传入匿名函数对数组清晰、便捷进行操作

eg:

// 创建集合 collect(\[1, 2, 3\]);

// 返回该集合所代表的底层数组： $collection-&gt;all();

// 返回集合中所有项目的平均值： $collection-&gt;avg();

// 将集合拆成多个给定大小的较小集合： $collection-&gt;chunk(4);

// 将多个数组组成的集合折成单一数组集合： $collection-&gt;collapse();

// 用来判断该集合是否含有指定的项目： $collection-&gt;contains('New York');

// 返回该集合内的项目总数： $collection-&gt;count();

// 遍历集合中的项目，并将之传入给定的回调函数： $collection = $collection-&gt;each(function ($item, $key) { });

// 会创建一个包含第 n 个元素的新集合： $collection-&gt;every(4);

// 传递偏移值作为第二个参数： $collection-&gt;every(4, 1);

// 返回集合中排除指定键的所有项目： $collection-&gt;except(\['price', 'discount'\]);

// 以给定的回调函数筛选集合，只留下那些通过判断测试的项目： $filtered = $collection-&gt;filter(function ($item) { return $item &gt; 2; });

// 返回集合中，第一个通过给定测试的元素： collect(\[1, 2, 3, 4\])-&gt;first(function ($key, $value) { return $value &gt; 2; });

// 将多维集合转为一维集合： $flattened = $collection-&gt;flatten();

// 将集合中的键和对应的数值进行互换： $flipped = $collection-&gt;flip();

// 以键自集合移除掉一个项目： $collection-&gt;forget('name');

// 返回含有可以用来在给定页码显示项目的新集合： $chunk = $collection-&gt;forPage(2, 3);

// 返回给定键的项目。如果该键不存在，则返回 null： $value = $collection-&gt;get('name');

// 根据给定的键替集合内的项目分组： $grouped = $collection-&gt;groupBy('account\_id');

// 用来确认集合中是否含有给定的键： $collection-&gt;has('email');

// 用来连接集合中的项目 $collection-&gt;implode('product', ', ');

// 移除任何给定数组或集合内所没有的数值： $intersect = $collection-&gt;intersect(\['Desk', 'Chair', 'Bookcase'\]);

// 假如集合是空的，isEmpty 方法会返回 true： collect(\[\])-&gt;isEmpty();

// 以给定键的值作为集合项目的键： $keyed = $collection-&gt;keyBy('product\_id');

// 传入回调函数，该函数会返回集合的键的值： $keyed = $collection-&gt;keyBy(function ($item) { return strtoupper($item\['product\_id'\]); });

// 返回该集合所有的键： $keys = $collection-&gt;keys();

// 返回集合中，最后一个通过给定测试的元素： $collection-&gt;last();

// 遍历整个集合并将每一个数值传入给定的回调函数： $multiplied = $collection-&gt;map(function ($item, $key) { return $item \* 2; });

// 返回给定键的最大值： $max = collect(\[\['foo' =&gt; 10\], \['foo' =&gt; 20\]\])-&gt;max('foo'); $max = collect(\[1, 2, 3, 4, 5\])-&gt;max();

// 将给定的数组合并进集合： $merged = $collection-&gt;merge(\['price' =&gt; 100, 'discount' =&gt; false\]);

// 返回给定键的最小值： $min = collect(\[\['foo' =&gt; 10\], \['foo' =&gt; 20\]\])-&gt;min('foo'); $min = collect(\[1, 2, 3, 4, 5\])-&gt;min();

// 返回集合中指定键的所有项目： $filtered = $collection-&gt;only(\['product\_id', 'name'\]);

// 获取所有集合中给定键的值： $plucked = $collection-&gt;pluck('name');

// 移除并返回集合最后一个项目： $collection-&gt;pop();

// 在集合前面增加一个项目： $collection-&gt;prepend(0);

// 传递第二个参数来设置前置项目的键： $collection-&gt;prepend(0, 'zero');

// 以键从集合中移除并返回一个项目： $collection-&gt;pull('name');

// 附加一个项目到集合后面： $collection-&gt;push(5);

// put 在集合内设置一个给定键和数值： $collection-&gt;put('price', 100);

// 从集合中随机返回一个项目： $collection-&gt;random();

// 传入一个整数到 random。如果该整数大于 1，则会返回一个集合： $random = $collection-&gt;random(3);

// 会将每次迭代的结果传入到下一次迭代： $total = $collection-&gt;reduce(function ($carry, $item) { return $carry + $item; });

// 以给定的回调函数筛选集合： $filtered = $collection-&gt;reject(function ($item) { return $item &gt; 2; });

// 反转集合内项目的顺序： $reversed = $collection-&gt;reverse();

// 在集合内搜索给定的数值并返回找到的键： $collection-&gt;search(4);

// 移除并返回集合的第一个项目： $collection-&gt;shift();

// 随机排序集合的项目： $shuffled = $collection-&gt;shuffle();

// 返回集合从给定索引开始的一部分切片： $slice = $collection-&gt;slice(4);

// 对集合排序： $sorted = $collection-&gt;sort();

// 以给定的键排序集合： $sorted = $collection-&gt;sortBy('price');

// 移除并返回从指定的索引开始的一小切片项目： $chunk = $collection-&gt;splice(2);

// 返回集合内所有项目的总和： collect(\[1, 2, 3, 4, 5\])-&gt;sum();

// 返回有着指定数量项目的集合： $chunk = $collection-&gt;take(3);

// 将集合转换成纯 PHP 数组： $collection-&gt;toArray();

// 将集合转换成 JSON： $collection-&gt;toJson();

// 遍历集合并对集合内每一个项目调用给定的回调函数： $collection-&gt;transform(function ($item, $key) { return $item \* 2; });

// 返回集合中所有唯一的项目： $unique = $collection-&gt;unique();

// 返回键重设为连续整数的的新集合： $values = $collection-&gt;values();

// 以一对给定的键／数值筛选集合： $filtered = $collection-&gt;where('price', 100);

// 将集合与给定数组同样索引的值合并在一起： $zipped = $collection-&gt;zip(\[100, 200\]);

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

3365d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/aa4e7eb298fdc023fd9f09ba3edbad50197de9757998420dcdb5c412b49a37ed?d=identicon)[wangjia5693](/maintainers/wangjia5693)

---

Top Contributors

[![wangjia5693](https://avatars.githubusercontent.com/u/8287270?v=4)](https://github.com/wangjia5693 "wangjia5693 (5 commits)")

### Embed Badge

![Health badge](/badges/wj-array-collection/health.svg)

```
[![Health](https://phpackages.com/badges/wj-array-collection/health.svg)](https://phpackages.com/packages/wj-array-collection)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
