Collection
class Collection implements ArrayAccess, ArrayAble, Countable, IteratorAggregate, Jsonable, JsonSerializable
Class Collection
Most of the methods in this file come from illuminate/support, thanks Laravel Team provide such a useful class.
Traits
Properties
protected array | $items | 元素 | from ArrayJsonSpl |
static protected array | $proxies | 可以被代理的方法 | from EnumeratesValues |
Methods
判断集合是否为空
如果适用,将给定值包装在集合中
从给定集合中获取基础项(如果适用)
contains方法的别名
验证集合中的每一个元素是否通过指定的条件测试
集合中含有指定键 / 值对的第一个元素
用来分开通过指定条件的元素以及那些不通过指定条件的元素
如果值为true,则应用回调
如果集合为空,则应用回调
如果集合不为空,则应用回调
如果值错误,则应用回调
除非集合为空,否则应用回调
除非集合不为空,否则应用回调
根据字段条件过滤数组中的元素
IN过滤
NOT IN过滤
过滤集合中的元素。如果回调函数返回 true 就会把对应的集合项从集合中移除
Get a CachingIterator instance.
计算集合中每个值的出现次数。默认情况下,该方法计算每个元素的出现次数
获取where回调
Create a new collection.
获取集合中的所有元素
获取集合中元素的个数
与给定列表交叉联接,返回所有可能的排列。
获取集合中不在给定项中的项。
获取集合中键和值不在给定项中的项。
获取集合中键和值不在给定项中的项。
获取集合中键不在给定项中的项。
获取集合中不在给定项中的项。
使用回调函数获取集合中键不在给定项中的项
从集合中检索重复项
使用严格比较从集合中检索重复项
获取比较函数以检测重复项
获取给定元素的平均值
通过字段或使用回调为关联数组设置键。
获取集合项的key
将集合中的值用字符串连接
取集合中的最后一个元素
对集合执行array_pop
对集合执行array_shift
push一个元素
打乱集合并返回结果
Put一个元素slice
从集合中随机获取一个或指定数量的项
将项目推到集合的开头
从集合中获取并移除项
reduce集合
返回顺序相反的集合
在集合中搜索给定的值并返回它的键。如果没有找到,则返回 false
返回除了给定的集合项数目的新集合
slice集合
分割成集合组
取第一个或最后一个{$limit}项
通过调用给定次数的回调来创建新集合
将集合分块
将集合折叠为单个数组。
返回数据中指定的一列
通过对键使用此集合,对其值使用另一个集合来创建集合。
过滤集合中的元素。只保留回调函数返回true的元素,如果没有提供回调函数,集合中所有返回 false 的元素都会被移除
从集合中获取第一个项目。
判断集合是否包含指定的集合项
将多维数组展平到单个级别。
翻转集合中的项目。
按键从集合中移除项
使用给定的回调排序集合。
使用给定的倒序回调排序集合
使用key对集合进行排序
使用key对集合进行排序-倒序
去掉集合中的某一部分并用其它值取代
类似 merge,但是,不仅可以覆盖匹配到的相同字符串键的集合项,而且也可以覆盖数字键的集合项:
类似 replace, 但是会以递归的形式将数组替换到具有相同键的集合项中
使用集合中的值组成新的集合
获取key获取集合的元素
通过字段或使用回调对关联数组进行分组
通过键确定集合中是否存在项。
将给定键的值连接为字符串
使集合与给定项相交
按键将集合与给定项相交
获取给定key的值
获取除具有指定键的项以外的所有项。
用回调函数处理数组中的元素
在项目上运行字典 map 回调应返回具有单个键/值对的关联数组。
对每个项运行关联映射。 回调应返回具有单个键/值对的关联数组。
使用回调对每个项进行排序。
获取给定key的中间值。
将集合与给定项合并。
递归地将集合与给定项合并
获取给定key的众数
创建一个包含每个第n个元素的新集合。
返回集合中所有指定键的集合项
使用值将集合填充到指定长度
使用回调转换集合中的每个项
将集合与给定项合并
将集合与一个或多个数组压缩在一起
Details
in ArrayJsonSpl at line 31
array
jsonSerialize()
将元素转换为可序列化JSON
at line 237
bool
isEmpty()
判断集合是否为空
in ArrayJsonSpl at line 64
false|string
toJson(int $options = JSON_UNESCAPED_UNICODE)
获取元素转换成json的结果
in ArrayJsonSpl at line 76
bool
offsetExists(mixed $key)
判断元素是否存在
in ArrayJsonSpl at line 88
mixed
offsetGet(mixed $key)
获取一个元素
in ArrayJsonSpl at line 99
offsetSet(mixed $key, mixed $value)
设置一个元素
in ArrayJsonSpl at line 113
offsetUnset(string $key)
Unset一个元素
in ArrayJsonSpl at line 123
array
toArray()
当前集合转换成数组返回
in ArrayJsonSpl at line 135
ArrayIterator
getIterator()
获取元素的迭代器
in ArrayJsonSpl at line 140
__toString()
in EnumeratesValues at line 68
static EnumeratesValues
make(mixed $items = [])
实例化对象
at line 625
static Collection
wrap($value)
如果适用,将给定值包装在集合中
at line 1206
static array
unwrap($value)
从给定集合中获取基础项(如果适用)
in EnumeratesValues at line 105
mixed
average(callable|string|null $callback = null)
avg 的别名
in EnumeratesValues at line 119
bool
some(mixed $key, mixed $operator = null, mixed $value = null)
contains方法的别名
in EnumeratesValues at line 132
bool
containsStrict(mixed $key, mixed $value = null)
判断集合是否包含指定的集合项 「严格」
in EnumeratesValues at line 159
void
dd(mixed ...$args)
打印items并结束
in EnumeratesValues at line 170
$this
dump()
打印items
in EnumeratesValues at line 188
$this
each(callable $callback)
给每个元素执行个回调
in EnumeratesValues at line 206
EnumeratesValues
eachSpread(callable $callback)
对每个嵌套的项块执行回调
in EnumeratesValues at line 224
bool
every(string|callable $key, mixed $operator = null, mixed $value = null)
验证集合中的每一个元素是否通过指定的条件测试
in EnumeratesValues at line 250
mixed
firstWhere(string $key, mixed $operator = null, mixed $value = null)
集合中含有指定键 / 值对的第一个元素
in EnumeratesValues at line 260
bool
isNotEmpty()
判断集合是否不为空
in EnumeratesValues at line 272
$this
mapSpread(callable $callback)
在每个嵌套的项上运行map
in EnumeratesValues at line 288
$this
mapToGroups(callable $callback)
对项目运行分组map 回调应返回具有单个键/值对的关联数组。
in EnumeratesValues at line 301
$this
flatMap(callable $callback)
映射集合并将结果展平一个级别。
in EnumeratesValues at line 313
EnumeratesValues
mapInto(string $class)
迭代元素到一个新的类中
in EnumeratesValues at line 327
mixed
min(callable|string|null $callback = null)
获取给定key的最小值
in EnumeratesValues at line 347
mixed
max(callable|string|null $callback = null)
获取给定key的最大值
in EnumeratesValues at line 368
$this
forPage(int $page, int $perPage)
通过将集合分割成较小的集合来“分页”。
in EnumeratesValues at line 383
EnumeratesValues
partition(callable|string $key, mixed $operator = null, mixed $value = null)
用来分开通过指定条件的元素以及那些不通过指定条件的元素
in EnumeratesValues at line 410
mixed
sum(callable|string|null $callback = null)
获取给定元素的和
in EnumeratesValues at line 434
$this
when(bool $value, callable $callback, callable $default = null)
如果值为true,则应用回调
in EnumeratesValues at line 453
EnumeratesValues|mixed
whenEmpty(callable $callback, callable $default = null)
如果集合为空,则应用回调
in EnumeratesValues at line 466
EnumeratesValues|mixed
whenNotEmpty(callable $callback, callable $default = null)
如果集合不为空,则应用回调
in EnumeratesValues at line 480
$this
unless(bool $value, callable $callback, callable $default = null)
如果值错误,则应用回调
in EnumeratesValues at line 492
EnumeratesValues|mixed
unlessEmpty(callable $callback, callable $default = null)
除非集合为空,否则应用回调
in EnumeratesValues at line 504
EnumeratesValues|mixed
unlessNotEmpty(callable $callback, callable $default = null)
除非集合不为空,否则应用回调
in EnumeratesValues at line 518
EnumeratesValues
where(string $key, mixed $operator = null, mixed $value = null)
根据字段条件过滤数组中的元素
in EnumeratesValues at line 531
EnumeratesValues
whereStrict(string $key, mixed $value)
根据字段条件过滤数组中的元素 全等
in EnumeratesValues at line 545
EnumeratesValues
whereIn(string $key, array $values, bool $strict = false)
IN过滤
in EnumeratesValues at line 562
EnumeratesValues
whereInStrict(string $key, array $values)
IN过滤-严格模式
in EnumeratesValues at line 576
EnumeratesValues
whereNotIn(string $key, array $values, bool $strict = false)
NOT IN过滤
in EnumeratesValues at line 592
EnumeratesValues
whereNotInStrict(string $key, array $values)
Not IN过滤-严格模式
in EnumeratesValues at line 604
EnumeratesValues
whereInstanceOf(mixed $type)
InstanceOf过滤
in EnumeratesValues at line 619
EnumeratesValues
whereBetween(string $key, mixed $values)
BETWEEN 过滤
in EnumeratesValues at line 632
EnumeratesValues
whereNotBetween(string $key, mixed $values)
NOT BETWEEN 过滤
in EnumeratesValues at line 648
EnumeratesValues
whereLike(string $key, string $value)
like 过滤
in EnumeratesValues at line 661
EnumeratesValues
whereNotLike(string $key, string $value)
not like过滤
in EnumeratesValues at line 673
mixed
pipe(callable $callback)
将集合传递给给定的回调并返回结果
in EnumeratesValues at line 685
EnumeratesValues
tap(callable $callback)
将集合传递给给定的回调,然后返回它
in EnumeratesValues at line 698
EnumeratesValues
reject(callable|mixed $callback = true)
过滤集合中的元素。如果回调函数返回 true 就会把对应的集合项从集合中移除
in EnumeratesValues at line 717
$this
unique(null $key = null, bool $strict = false)
仅返回集合数组中的唯一项
in EnumeratesValues at line 737
$this
uniqueStrict(null $key = null)
使用严格比较仅返回集合数组中的唯一项
in EnumeratesValues at line 747
Collection
collect()
返回一个新的 Collection 实例,其中包含当前集合中的项目
in EnumeratesValues at line 760
CachingIterator
getCachingIterator(int $flags = CachingIterator::CALL_TOSTRING)
Get a CachingIterator instance.
in EnumeratesValues at line 772
EnumeratesValues
countBy(callable|null $callback = null)
计算集合中每个值的出现次数。默认情况下,该方法计算每个元素的出现次数
in EnumeratesValues at line 794
static void
proxy(string $method)
将方法添加到代理方法的列表中。
in EnumeratesValues at line 806
mixed
__get(string $key)
动态访问收集代理
in EnumeratesValues at line 825
protected Closure
operatorForWhere(string $key, mixed $operator = null, mixed $value = null)
获取where回调
in EnumeratesValues at line 878
protected bool
useAsCallable(mixed $value)
判断是否为callable
in EnumeratesValues at line 890
protected Closure|mixed
valueRetriever(mixed $value)
获取检索回调的值。
in EnumeratesValues at line 907
protected mixed
getArrayAbleItems(mixed $items)
处理元素返回array
at line 39
__construct(mixed $items = [])
Create a new collection.
at line 48
all()
获取集合中的所有元素
at line 58
count()
获取集合中元素的个数
at line 70
Collection
crossJoin(array ...$lists)
与给定列表交叉联接,返回所有可能的排列。
at line 82
Collection
diff(mixed $items)
获取集合中不在给定项中的项。
at line 94
Collection
diffAssoc(mixed $items)
获取集合中键和值不在给定项中的项。
at line 107
Collection
diffAssocUsing(mixed $items, callable $callback)
获取集合中键和值不在给定项中的项。
at line 119
Collection
diffKeys(mixed $items)
获取集合中键不在给定项中的项。
at line 132
Collection
diffUsing(mixed $items, callable $callback)
获取集合中不在给定项中的项。
at line 145
Collection
diffKeysUsing(mixed $items, callable $callback)
使用回调函数获取集合中键不在给定项中的项
at line 158
Collection
duplicates(callable|null $callback = null, bool $strict = false)
从集合中检索重复项
at line 186
Collection
duplicatesStrict(callable|null $callback = null)
使用严格比较从集合中检索重复项
at line 198
protected Closure
duplicateComparator(bool $strict)
获取比较函数以检测重复项
at line 218
float|int
avg(null|callable|string $callback = null)
获取给定元素的平均值
at line 249
$this
keyBy(callable|string $keyBy)
通过字段或使用回调为关联数组设置键。
at line 268
$this
keys()
获取集合项的key
at line 281
string
join(string $glue, string $finalGlue = '')
将集合中的值用字符串连接
at line 312
mixed|null
last(callable $callback = null, null $default = null)
取集合中的最后一个元素
at line 322
mixed
pop()
对集合执行array_pop
at line 332
mixed
shift()
对集合执行array_shift
at line 344
$this;
push(mixed $value)
push一个元素
at line 357
$this
shuffle(int|null $seed = null)
打乱集合并返回结果
at line 370
$this;
put(mixed $key, mixed $value)
Put一个元素slice
at line 383
Collection
random(int|null $number = null)
从集合中随机获取一个或指定数量的项
at line 399
$this;
prepend($value, $key = null)
将项目推到集合的开头
at line 413
mixed
pull($key, $default = null)
从集合中获取并移除项
at line 425
Collection
concat(array|Traversable $source)
合并已有集合
at line 442
mixed
reduce(callable $callback, mixed $initial = null)
reduce集合
at line 451
reverse()
返回顺序相反的集合
at line 464
mixed
search(mixed $value, bool $strict = false)
在集合中搜索给定的值并返回它的键。如果没有找到,则返回 false
at line 484
Collection
skip(int $count)
返回除了给定的集合项数目的新集合
at line 497
$this
slice(int $offset, int|null $length = null)
slice集合
at line 509
Collection
split(int $numberOfGroups)
分割成集合组
at line 538
Collection
take(int $limit)
取第一个或最后一个{$limit}项
at line 554
static Collection
times(int $number, callable $callback = null)
通过调用给定次数的回调来创建新集合
at line 572
Collection
chunk(int $size)
将集合分块
at line 588
collapse()
将集合折叠为单个数组。
at line 601
array
column(string $columnKey, string $indexKey = null)
返回数据中指定的一列
at line 613
$this
combine($values)
通过对键使用此集合,对其值使用另一个集合来创建集合。
at line 637
$this
filter(callable $callback)
过滤集合中的元素。只保留回调函数返回true的元素,如果没有提供回调函数,集合中所有返回 false 的元素都会被移除
at line 650
mixed
first(callable $callback = null, null $default = null)
从集合中获取第一个项目。
at line 664
bool
contains(mixed $key, mixed $operator = null, mixed $value = null)
判断集合是否包含指定的集合项
at line 686
$this
flatten(int $depth = INF)
将多维数组展平到单个级别。
at line 696
flip()
翻转集合中的项目。
at line 708
$this
forget(mixed $keys)
按键从集合中移除项
at line 725
Collection
sortBy($callback, int $options = SORT_REGULAR, bool $descending = false)
使用给定的回调排序集合。
at line 753
$this
sortByDesc($callback, int $options = SORT_REGULAR)
使用给定的倒序回调排序集合
at line 766
$this
sortKeys(int $options = SORT_REGULAR, bool $descending = false)
使用key对集合进行排序
at line 780
$this
sortKeysDesc(int $options = SORT_REGULAR)
使用key对集合进行排序-倒序
at line 794
$this
splice(int $offset, int|null $length = null, array $replacement = [])
去掉集合中的某一部分并用其它值取代
at line 809
Collection
replace(mixed $items)
类似 merge,但是,不仅可以覆盖匹配到的相同字符串键的集合项,而且也可以覆盖数字键的集合项:
at line 821
Collection
replaceRecursive(mixed $items)
类似 replace, 但是会以递归的形式将数组替换到具有相同键的集合项中
at line 831
$this
values()
使用集合中的值组成新的集合
at line 844
mixed
get($key, $default = null)
获取key获取集合的元素
at line 860
$this
groupBy($groupBy, bool $preserveKeys = false)
通过字段或使用回调对关联数组进行分组
at line 895
bool
has(mixed $key)
通过键确定集合中是否存在项。
at line 914
string
implode($value, null $glue = null)
将给定键的值连接为字符串
at line 930
$this
intersect($items)
使集合与给定项相交
at line 942
$this
intersectByKeys($items)
按键将集合与给定项相交
at line 955
$this
pluck($value, string|null $key = null)
获取给定key的值
at line 967
$this
except($keys)
获取除具有指定键的项以外的所有项。
at line 984
Collection
map(callable $callback)
用回调函数处理数组中的元素
at line 999
$this
mapToDictionary(callable $callback)
在项目上运行字典 map 回调应返回具有单个键/值对的关联数组。
at line 1022
$this
mapWithKeys(callable $callback)
对每个项运行关联映射。 回调应返回具有单个键/值对的关联数组。
at line 1041
Collection
sort(callable $callback = null)
使用回调对每个项进行排序。
at line 1055
mixed
median(string $key = null)
获取给定key的中间值。
at line 1081
Collection
merge(mixed $items)
将集合与给定项合并。
at line 1093
Collection
mergeRecursive(mixed $items)
递归地将集合与给定项合并
at line 1105
null|array
mode(null|mixed $key = null)
获取给定key的众数
at line 1130
Collection
nth(int $step, int $offset = 0)
创建一个包含每个第n个元素的新集合。
at line 1150
Collection
only($keys)
返回集合中所有指定键的集合项
at line 1170
Collection
pad(int $size, mixed $value)
使用值将集合填充到指定长度
at line 1181
$this
transform(callable $callback)
使用回调转换集合中的每个项
at line 1194
Collection
union($items)
将集合与给定项合并
at line 1221
Collection
zip(mixed $items)
将集合与一个或多个数组压缩在一起
e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); => [[1, 4], [2, 5], [3, 6]].