一致的数组函数
Requires
- php: >=7.4
- symfony/polyfill-php81: ^1.24
Requires (Dev)
- phpunit/phpunit: ^8
- symfony/var-dumper: ^5.0
This package is auto-updated.
Last update: 2024-09-09 11:17:25 UTC
README
Ar 使处理 PHP 数组变得容易
- 一致性:所有函数都将数组作为第一个参数接受。
- 不可变:输入数组永远不会被修改。流畅风格在每次调用后都返回一个新的对象。
- 已测试:使用 100% 代码覆盖率进行单元测试。
- 熟悉:函数名称尽可能遵循 PHP。
流畅风格
use FriendlyPixel\Ar\Ar; $ints = Ar::wrap([1, 6, 8]) ->map(fn ($num) => $num * $num) ->filter(fn ($value, $key) => $value % 2 == 0);
函数式风格
use FriendlyPixel\Ar\Ar; $ints = [1, 5, 8]; $ints = Ar::map($ints, fn($num) => $num * $num); $ints = Ar::filter($ints, fn($value, $key) => $value % 2 == 0)
安装
使用 Composer 安装最新版本
$ composer require friendly-pixel/ar
方法
- count()
- filter()
- first()
- flat()
- forEach()
- implode()
- keys()
- last()
- map()
- mapKeys()
- merge()
- push()
- reduce()
- search()
- slice()
- sort()
- splice()
- unique()
- unshift()
- values()
仅流畅风格
count
计算数组中有多少个元素。
use FriendlyPixel\Ar\Ar; $count = Ar::count([1, 2, 3]); $count = Ar::wrap([1, 2, 3]) ->count() ; // Result: 3
filter
将每个值、键传递给用户提供的可调用函数,如果返回值为 true
,则仅将项目放入结果数组。仅在 array_is_list($array)
返回 false 时保留键;
use FriendlyPixel\Ar\Ar; $even = Ar::filter([1, 2, 3, 12], function($value, $key) { return $value % 2 == 0; }); $even = Ar::wrap([1, 2, 3, 12]) ->filter(function($value, $key) { return $value % 2 == 0; }) ->unwrap(); // Result: [1 => 2, 3 => 12]
@template A
@param A[] $array
@param callable(A $value, mixed $key): bool $callable
@return A[]
first
返回数组的第一个值或为空时返回 false
。
use FriendlyPixel\Ar\Ar; Ar::first([2, 3, 4]); Ar::wrap([2, 3, 4])->first(); // Result: 2
@template A
@param A[] $array
@return A
flat
flat() 方法创建一个新的数组,其中包含递归地连接到指定深度的所有子数组元素。
@param int $depth 将数组扁平化到哪个级别。默认:1
@return mixed[]
forEach
遍历每个值、键。将每个值、键传递给用户提供的可调用函数。
@template A
@param A[] $array
@param callable(A $value, mixed $key): void $callable
@return A[] 原始数组,未修改
implode
使用 $glue
作为分隔符将所有值连接成一个长字符串。 $glue
是可选的。
use FriendlyPixel\Ar\Ar; $result = Ar::implode(['a', 'b', 'c'], ','); $result = Ar::wrap(['a', 'b', 'c']) ->implode(',') ; // result: "a,b,c"
keys
返回数组的键作为顺序数组。
use FriendlyPixel\Ar\Ar; $result = Ar::keys([3 => 'a', 'foo' => 'b', 1 => 'c']); $result = Ar::wrap([3 => 'a', 'foo' => 'b', 1 => 'c'])->keys()->unwrap(); // result: [3, 'foo', 1]
@return mixed[]
last
返回数组的最后一个值或为空时返回 false
。
use FriendlyPixel\Ar\Ar; Ar::last([2, 3, 4]); Ar::wrap([2, 3, 4])->last(); // Result: 4
@template A
@param A[] $array
@return A
map
转换值。将每个值、键传递给用户提供的可调用函数,并将返回的值放入结果数组。键被保留。
use FriendlyPixel\Ar\Ar; $numbers = Ar::map([1, 2, 3], function($value, $key) { return $value * 2; }); $numbers = Ar::wrap([1, 2, 3]) ->map(function($value, $key) { return $value * 2; }) ->unwrap(); // Result: [2, 4, 6]
@template A
@template B
@param A[] $array
@param callable(A $value, mixed $key): B $callable
@return B[]
mapKeys
转换键。将每个值、键和键传递给用户提供的可调用函数,并使用返回的值作为结果数组中的键。
use FriendlyPixel\Ar\Ar; $numbers = Ar::mapKeys([1, 2, 3], function($value, $key) { return $key * 2; }); $numbers = Ar::wrap([1, 2, 3]) ->mapKeys(function($value, $key) { return $key * 2; }) ->unwrap(); // Result: [0 => 2, 2 => 2, 4 => 3]
@template A
@template K
@param A[] $array
@param callable(A $value, mixed $key): K $callable
@return array
merge
将一个或多个数组的元素合并在一起,使一个数组的值追加到前一个数组的末尾。如果输入数组有相同的字符串键,则该键的较晚值将覆盖之前的值。但是,如果数组包含数字键,则较晚的值不会覆盖原始值,而是追加。输入数组中具有数字键的值将在结果数组中重新编号,从零开始递增。
use FriendlyPixel\Ar\Ar; $numbers = Ar::merge(['a', 'b'], ['c', 'd'])); $numbers = Ar::wrap(['a', 'b']) ->merge(['b', 'c']) ->unwrap(); // Result:['a', 'b', 'c', 'd']
@template A
@var A[][] $arrays
@return A[]
push
将一个或多个项追加到数组的末尾。
use FriendlyPixel\Ar\Ar; $result = Ar::push([1, 2], 3, 4); $result = Ar::wrap([1, 2])->push(3, 4)->unwrap(); // result: [1, 2, 3, 4]
@template A
@param A[] $array
@param A[] $values
@return A[]
reduce
使用回调函数迭代地将数组减少到单个值。
@template A
@template B
@param A[] $array
@param callable(B|null $carry, A $value, mixed $key): B $callable
@param B|null $initial 如果提供了可选的初始值,它将在处理开始时使用,或者如果数组为空,则作为最终结果。
@return B
search
返回第一个使可调用返回 true
的值。否则返回 null
。
use FriendlyPixel\Ar\Ar; $found = Ar::search([ ['a' => 1], ['a' => 8], ['a' => 3] ], function($value, $key) { return $value['a'] == 3; }); $found = Ar::wrap([ ['a' => 1], [], ['a' => 3] ]) ->search(function($value, $key) { return $value['a'] == 3; }) ; // Result: ['a' => 3]
@template A
@param A[] $array
@param callable(A $value, mixed $key): bool $callable
@return A|null
slice
提取数组的切片,包含 $length
项,并从 $offset
开始。
use FriendlyPixel\Ar\Ar; $even = Ar::slice(['a', 'b', 'c', 'd'], 1, 2); $even = Ar::wrap(['a', 'b', 'c', 'd']) ->slice(1, 2) ->unwrap(); // Result: ['b', 'c']
@template A
@param A[] $array
@param int $offset 如果偏移量是非负的,则序列将从数组中的该偏移量开始。如果偏移量是负的,则序列将从数组末尾开始该偏移量。
@param ?int $length 如果提供了长度并且是正数,则序列将包含最多那么多元素。如果数组长度小于长度,则只包含数组中的可用元素。如果提供了长度并且是负数,则序列将停止在数组末尾之前的那么多元素。如果省略,则序列将从偏移量开始直到数组末尾。
@return A[]
sort
使用用户定义的比较函数对数组进行排序。
此函数为新数组元素分配新键。它将删除可能已分配的任何现有键。
@template A
@param A[] $array
@param callable(A $valueA, A $valueB): int $callable
返回一个整数,小于、等于或大于0,以指示 $valueA 小于、等于或大于 $valueB。
@return A[]
splice
删除数组的一部分并用其他内容替换。与默认的 php 函数不同,这个函数返回更改后的数组,而不是提取的元素。
use FriendlyPixel\Ar\Ar; $even = Ar::splice(['a', 'b', 'c', 'd'], 1, 1, ['q', 'x']); $even = Ar::wrap(['a', 'b', 'c', 'd']) ->splice(1, 1, ['q', 'x']) ->unwrap(); // Result: ['a', 'q', 'x', 'c', 'd']
@template A
@param A[] $array
@param int $offset 如果偏移量是正数,则删除部分的起始位置是数组开始的偏移量。
If offset is negative then the start of the removed portion is at that offset from
the end of the array.
@param ?int $length 如果省略长度,则从偏移量删除到数组末尾的所有内容。* 如果指定了长度并且是正数,则将删除那么多元素。
If length is specified and is negative, then the end of the removed portion will be
that many elements from the end of the array.
If length is specified and is zero, no elements will be removed.
@param A[] $replacement 如果指定了替换数组,则删除的元素将用此数组的元素替换。
If offset and length are such that nothing is removed, then the elements from the
replacement array are inserted in the place specified by the offset.
*
If replacement is just one element it is not necessary to put array() or square brackets
around it, unless the element is an array itself, an object or null.
Note: Keys in the replacement array are not preserved.
@return A[] 与默认的 php 函数不同,此函数返回更改后的数组,而不是提取的元素。
unique
从数组中删除重复的值。仅在 array_is_list($array)
返回 false 时保留键;
use FriendlyPixel\Ar\Ar; $result = Ar::unique(['a', 'a', 'b']); $result = Ar::wrap(['b', 4])->unique(['a', 'a', 'b'])->unwrap(); // result: [0 => 'a', 2 => 'b']
@template A
@param A[] $array
@return A[]
unshift
在数组的开始处预置一个或多个项目。
use FriendlyPixel\Ar\Ar; $result = Ar::unshift([3, 4], 1, 2); $result = Ar::wrap([3, 4])->unshift(1, 2)->unwrap(); // result: [1, 2, 3, 4]
@template A
@param A[] $array
@param A[] $values
@return A[]
values
返回数组作为顺序数组的值。
use FriendlyPixel\Ar\Ar; $result = Ar::values([3 => 'a', 'foo' => 'b', 1 => 'c']); $result = Ar::wrap([3 => 'a', 'foo' => 'b', 1 => 'c'])->values()->unwrap(); // result: [0 => 'a', 1 => 'b', 2 => 'c']
@template A
@param array<mixed, A> $array
@return array<int, A>
仅流式风格方法
wrap
包装一个数组,这样您就可以使用流畅的语法调用它的多个方法。如果需要纯数组,请使用 ->unwrap()
。
use FriendlyPixel\Ar\Ar; $numbers = Ar::wrap([1, 2, 3]) ->map(function ($value, $key) { return $value * 2; }) ->filter(function ($value) { return $value != 6; }) ->unwrap() ; // If you don't like the Ar::wrap syntax, you can also use ArFluent directly: use FriendlyPixel\Ar\ArFluent; $numbers = (new ArFluent([1, 2, 3])) ->map(function ($value, $key) { return $value * 2; }) ->filter(function ($value) { return $value != 6; }) ->unwrap() ;
unwrap
返回基础数组。
use FriendlyPixel\Ar\Ar; $numbers = Ar::wrap([1, 2, 3]) ->map(function ($value, $key) { return $value * 2; }) ->unwrap() ; // Result: [2, 4, 6]
toArray
unwrap() 的别名