arsalanthange / php-collections
一个简单的PHP集合类,使您更容易处理数组。灵感来源于Laravel Collections。
v1.0.0
2019-04-21 05:22 UTC
Requires
- php: >=7.1
This package is auto-updated.
Last update: 2024-09-06 22:40:27 UTC
README
此库将您的数组转换为集合。它附带各种内置函数来操作和处理数组。该类是不可变的,这意味着您执行的每个集合方法都将返回一个新的集合实例。
用法
将数组转换为集合
// Array which is to be converted to a Collection $array = [5, 'a', 't', 2, 7]; $collection = new Collection($array);
排序集合
//Sort in Ascending Order $collection->sort()->all(); //Output: ['a', 't', 2, 5, 7] //Sort in Descending Order $collection->sort('DESC')->all(); //Output: [7, 5, 2, 't', 'a']
分页集合
注意:此方法不会返回一个 Collection
实例。 paginate
方法接受 2 个参数。
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; $collection = new Collection($array); //Page number $page = 1; //Number of elements to be displayed on the page $count = 4 //Paginate the collection $collection->paginate($page, $count); /*Output: [ 'total' => 20, 'current_page' => 1, 'total_pages' => 5, 'from' => 1, 'to' => 4, 'data' => [1, 2, 3, 4] ] */
如果您需要下一页的元素,只需增加页面值。注意: paginate
方法应在初始集合上运行以进行页面增加,而不是在返回的新实例上。
过滤集合
过滤适用于所有基本操作,如 =
、!=
、>
、<
、>=
和 <=
。
$array = [ ['apples' => 5, 'bananas' => 3, 'oranges' => 2], ['apples' => 10, 'bananas' => 5, 'oranges' => 1], ['apples' => 15, 'bananas' => 10, 'oranges' => 6] ]; $collection = new Collection($array); //Filter the collection $collection->where('apples', '>', 5)->all(); /* Output: [ ['apples' => 10, 'bananas' => 5, 'oranges' => 1], ['apples' => 15, 'bananas' => 10, 'oranges' => 6] ] */
您可以将 where
方法链接起来以应用多个过滤器。
//Filter the collection $collection->where('apples', '>', 5)->where('bananas', '=', 10)->all(); /* Output: [ ['apples' => 15, 'bananas' => 10, 'oranges' => 6] ] */
计算集合中元素的数量
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; $collection = new Collection($array); //Count the number of elements $collection->count(); //Output: 10
重置数组键
values
方法重置您的数组键。
$array = [ 'first_basket' => ['apples' => 5, 'bananas' => 3, 'oranges' => 2], 'second_basket' => ['apples' => 10, 'bananas' => 5, 'oranges' => 1], 'third_basket' => ['apples' => 15, 'bananas' => 10, 'oranges' => 6] ]; $collection = new Collection($array); $collection->values()->all(); /*Output: [ ['apples' => 5, 'bananas' => 3, 'oranges' => 2], ['apples' => 10, 'bananas' => 5, 'oranges' => 1], ['apples' => 15, 'bananas' => 10, 'oranges' => 6] ] */
获取指定键的值
get
方法返回指定键的值。如果键不存在且未指定默认值,则返回 null
。
$array = [ 'first_basket' => ['apples' => 5, 'bananas' => 3, 'oranges' => 2], 'second_basket' => ['apples' => 10, 'bananas' => 5, 'oranges' => 1], 'third_basket' => ['apples' => 15, 'bananas' => 10, 'oranges' => 6] ]; $collection = new Collection($array); $collection->get('first_basket'); // Output: ['apples' => 5, 'bananas' => 3, 'oranges' => 2] $collection->get('fourth_basket'); // Output: null //Second parameter is the default value if the key does not exist $collection->get('fourth_basket', 'No fruits here!'); // Output: No fruits here!
集合中的元素求和
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; $collection = new Collection($array); //Sum elments $collection->sum(); //Output: 55
集合中的元素求平均值
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; $collection = new Collection($array); //Average elements $collection->avg(); //Output: 5.5
将集合中的元素连接成一个字符串
$array = [1, 2, 3, 4]; $collection = new Collection($array); //Join elements $collection->implode(); //Output: 1,2,3,4
如果您希望指定 glue
用于 implode
,则可以在 implode
方法中传递该值。
//Join elements $collection->implode('-'); //Output: 1-2-3-4
如果集合包含数组或对象,则必须传递您希望连接的属性的 key
,以及您希望在值之间放置的 glue
。
$array = [ [ 'foo' => 1, 'bar' => 2], [ 'foo' => 3, 'bar' => 4], [ 'foo' => 5, 'bar' => 6], ]; $collection = new Collection($array); //Join elements $collection->implode(',', 'foo'); //Output: 1,3,5