2017 © Pedro Peláez
 

library array-collection

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

image

wj/array-collection

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

  • Friday, March 3, 2017
  • by wangjia5693
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

清晰操作array

通过传入匿名函数对数组清晰、便捷进行操作, (*1)

eg:, (*2)

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

// 返回该集合所代表的底层数组: $collection->all();, (*4)

// 返回集合中所有项目的平均值: $collection->avg();, (*5)

// 将集合拆成多个给定大小的较小集合: $collection->chunk(4);, (*6)

// 将多个数组组成的集合折成单一数组集合: $collection->collapse();, (*7)

// 用来判断该集合是否含有指定的项目: $collection->contains('New York');, (*8)

// 返回该集合内的项目总数: $collection->count();, (*9)

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

// 会创建一个包含第 n 个元素的新集合: $collection->every(4);, (*11)

// 传递偏移值作为第二个参数: $collection->every(4, 1);, (*12)

// 返回集合中排除指定键的所有项目: $collection->except(['price', 'discount']);, (*13)

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

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

// 将多维集合转为一维集合: $flattened = $collection->flatten();, (*16)

// 将集合中的键和对应的数值进行互换: $flipped = $collection->flip();, (*17)

// 以键自集合移除掉一个项目: $collection->forget('name');, (*18)

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

// 返回给定键的项目。如果该键不存在,则返回 null: $value = $collection->get('name');, (*20)

// 根据给定的键替集合内的项目分组: $grouped = $collection->groupBy('account_id');, (*21)

// 用来确认集合中是否含有给定的键: $collection->has('email');, (*22)

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

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

// 假如集合是空的,isEmpty 方法会返回 true: collect([])->isEmpty();, (*25)

// 以给定键的值作为集合项目的键: $keyed = $collection->keyBy('product_id');, (*26)

// 传入回调函数,该函数会返回集合的键的值: $keyed = $collection->keyBy(function ($item) { return strtoupper($item['product_id']); });, (*27)

// 返回该集合所有的键: $keys = $collection->keys();, (*28)

// 返回集合中,最后一个通过给定测试的元素: $collection->last();, (*29)

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

// 返回给定键的最大值: $max = collect([['foo' => 10], ['foo' => 20]])->max('foo'); $max = collect([1, 2, 3, 4, 5])->max();, (*31)

// 将给定的数组合并进集合: $merged = $collection->merge(['price' => 100, 'discount' => false]);, (*32)

// 返回给定键的最小值: $min = collect([['foo' => 10], ['foo' => 20]])->min('foo'); $min = collect([1, 2, 3, 4, 5])->min();, (*33)

// 返回集合中指定键的所有项目: $filtered = $collection->only(['product_id', 'name']);, (*34)

// 获取所有集合中给定键的值: $plucked = $collection->pluck('name');, (*35)

// 移除并返回集合最后一个项目: $collection->pop();, (*36)

// 在集合前面增加一个项目: $collection->prepend(0);, (*37)

// 传递第二个参数来设置前置项目的键: $collection->prepend(0, 'zero');, (*38)

// 以键从集合中移除并返回一个项目: $collection->pull('name');, (*39)

// 附加一个项目到集合后面: $collection->push(5);, (*40)

// put 在集合内设置一个给定键和数值: $collection->put('price', 100);, (*41)

// 从集合中随机返回一个项目: $collection->random();, (*42)

// 传入一个整数到 random。如果该整数大于 1,则会返回一个集合: $random = $collection->random(3);, (*43)

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

// 以给定的回调函数筛选集合: $filtered = $collection->reject(function ($item) { return $item > 2; });, (*45)

// 反转集合内项目的顺序: $reversed = $collection->reverse();, (*46)

// 在集合内搜索给定的数值并返回找到的键: $collection->search(4);, (*47)

// 移除并返回集合的第一个项目: $collection->shift();, (*48)

// 随机排序集合的项目: $shuffled = $collection->shuffle();, (*49)

// 返回集合从给定索引开始的一部分切片: $slice = $collection->slice(4);, (*50)

// 对集合排序: $sorted = $collection->sort();, (*51)

// 以给定的键排序集合: $sorted = $collection->sortBy('price');, (*52)

// 移除并返回从指定的索引开始的一小切片项目: $chunk = $collection->splice(2);, (*53)

// 返回集合内所有项目的总和: collect([1, 2, 3, 4, 5])->sum();, (*54)

// 返回有着指定数量项目的集合: $chunk = $collection->take(3);, (*55)

// 将集合转换成纯 PHP 数组: $collection->toArray();, (*56)

// 将集合转换成 JSON: $collection->toJson();, (*57)

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

// 返回集合中所有唯一的项目: $unique = $collection->unique();, (*59)

// 返回键重设为连续整数的的新集合: $values = $collection->values();, (*60)

// 以一对给定的键/数值筛选集合: $filtered = $collection->where('price', 100);, (*61)

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

The Versions

03/03 2017

dev-master

9999999-dev

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

  Sources   Download

MIT

by Avatar wangjia5693

21/02 2017

1.0.1

1.0.1.0

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

  Sources   Download

MIT

by Avatar wangjia5693

21/02 2017

1.0

1.0.0.0

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

  Sources   Download

MIT

by Avatar wangjia5693