dimsog / arrayhelper
适用于 PHP 5.4+ 的 ArrayHelper
1.2.3
2019-12-13 13:39 UTC
Requires
- php: >=5.4.0
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^4.8
README
适用于 PHP 5.4+ 的 ArrayHelper
支持的 PHP 版本
- PHP 5.4
- PHP 5.5
- PHP 5.6
- PHP 7.0
- PHP 7.1
- PHP 7.2
- PHP 7.3
- PHP 7.4
安装
您可以通过 composer 安装 ArrayHelper
composer require dimsog/arrayhelper
Packagist 链接 这里
升级信息
从 1.2.1 升级到 1.2.2
- Fluent::chunk($array, $column = 2) 转换为 Fluent::chunk($column = 2)
Fluent 接口
ArrayHelper::fluent($sourceArray) ->toInt(['id', 'parent_id']) ->except(['some_field']) ->filter(['user_id' => 100]) ->get(); // or Arr::fluent([[1], [2], [3]]) ->collapse() ->map(function($item) { return $item * 2; }) ->get();
简码
您可以使用 Arr 代替 ArrayHelper。
ArrayHelper::collapse([[1, 2, 3], [4, 5, 6]]); // or Arr::collapse([[1, 2, 3], [4, 5, 6]]);
可用方法
- camelCaseKeys
- collapse
- column
- chunk
- except
- exist
- filter
- findFirst
- firstKey
- getValue
- has
- ids
- insert
- isAssoc
- isMulti
- keyValue
- lastKey
- map
- only
- onlyWithKey
- paginate
- prepend
- random
- reindex
- remove
- replaceKey
- set
- shuffle
- sortBy
- sortByAsc
- sortByDesc
- splitString
- sum
- toArray
- toInt
- unique
- values
- wrap
代码示例
驼峰式键名
将 snake_case 键转换为 camelCase
ArrayHelper::camelCaseKeys(array $source)
演示
$data = ArrayHelper::camelCaseKeys([ 'demo_field' => 100 ]); // result ($data): [ 'demoField' => 100 ]
折叠
将数组数组折叠成一个单一数组
ArrayHelper::collapse(array $array)
演示
$result = ArrayHelper::collapse([[1, 2, 3], [4, 5, 6]]); result: [1, 2, 3, 4, 5, 6] $result = ArrayHelper::collapse([1, 2, 3, [4], [5, 6]]); result: [1, 2, 3, 4, 5, 6]
列
从输入数组返回单个列的值
ArrayHelper::column(array $array, $key)
演示
$array = [ [ 'id' => 1, 'name' => 'test1' ], [ 'id' => 2, 'name' => 'test2' ] ]; ArrayHelper::column($array, 'id'); result: [1, 2]
块
将数组分割成列
ArrayHelper::chunk(array $array, $column = 2)
演示
$array = [ 'foo' => 'bar', 'bar' => 'baz', 'vodka' => 'balalayka' ]; $result = ArrayHelper::chunk($array, 2) result: [ [ 'foo' => 'bar', 'bar' => 'baz' ], [ 'vodka' => 'balalayka' ] ];
除了
获取除 $keys 之外的给定数组的项目子集
ArrayHelper::except(array $array, array $keys)
演示
ArrayHelper::except(['a', 'b', 'c'], ['a', 'b']); result: ['c']
存在
此方法通过键、值或可调用检查存在或不存在值
ArrayHelper::exist(array $array, $condition)
演示
ArrayHelper::exist(['a', 'b', 'c'], 'b'); // true $array = [ [ 'id' => 100, 'name' => 'Product 1' ], [ 'id' => 200, 'name' => 'Product 2' ], [ 'id' => 300, 'name' => 'Product 3' ] ]; ArrayHelper::exist($array, ['id' => 200]); // true ArrayHelper::exist($array, function($item) { return $item['id'] == 300; });
过滤
过滤数组
ArrayHelper::filter(array $array, $condition, $preserveKeys = false)
演示
$array = [ [ 'id' => 1, 'category_id' => 5, 'name' => 'test1' ], [ 'id' => 3, 'category_id' => 1, 'name' => 'test3' ], ]; ArrayHelper::filter($array, ['category_id' => 5]); // OR ArrayHelper::filter($array, function($item) { return $item['category_id'] == 5; }); result: [ [ 'id' => 1, 'category_id' => 5, 'name' => 'test1' ] ]
找到第一个
从数组中找到一个第一个数组项
ArrayHelper::findFirst(array $array, $condition)
演示
$array = [ [ 'id' => 1, 'foo' => 'bar' ], [ 'id' => 2, 'foo' => 'baz' ], [ 'id' => 3, 'foo' => 'baz' ] ]; // find a first element using callback: ArrayHelper::findFirst($array, function($element) { return $element['foo'] == 'baz'; }); result: [ 'id' => 2, 'foo' => 'baz' ] // find a first element using array condition: ArrayHelper::findFirst($array, ['foo' => 'baz']); result: [ 'id' => 2, 'foo' => 'baz' ]
第一个键
获取给定数组的第一个键
ArrayHelper::firstKey(array $array)
演示
$array = [ 'a' => 1, 'b' => 2, 'c' => 3 ]; ArrayHelper::firstKey($array); // result: 'a'
插入
向现有数组插入新列
ArrayHelper::insert(&$array, $key, $value = null)
演示
$array = [ 'id' => 1, 'name' => 'Dmitry R' ]; ArrayHelper::insert($array, 'country', 'Russia'); result: [ 'id' => 1, 'name' => 'Dmitry R', 'country' => 'Russia' ]
$array = [ [ 'id' => 1, 'name' => 'Dmitry R' ], [ 'id' => 1, 'name' => 'Dmitry R' ] ]; ArrayHelper::insert($array, 'foo', 'bar'); result: [ [ 'id' => 1, 'name' => 'Dmitry R', 'foo' => 'bar' ], [ 'id' => 1, 'name' => 'Dmitry R', 'foo' => 'bar' ] ]
获取值
检索数组的值。您也可以使用 stdClass 代替数组。
ArrayHelper::getValue($array, $key, $defaultValue = null) ArrayHelper::get($array, $key, $defaultValue = null)
演示
ArrayHelper::getValue($user, 'id'); // with callback default value ArrayHelper::getValue($user, 'name', function() { return "Dmitry R"; }); // Retrivies the value of a sub-array $user = [ 'photo' => [ 'big' => '/path/to/image.jpg' ] ] ArrayHelper::getValue($user, 'photo.big'); // Retrivies the value of a stdClass $user = json_decode($userJsonString); ArrayHelper::getValue($user, 'photo.big');
has
此方法检查给定的键是否存在于数组中。您也可以使用点表示法。
ArrayHelper::has(array $array, $key)
演示
$array = [ 'foo' => [ 'bar' => 10 ] ]; ArrayHelper::has($array, 'foo.bar') // true
$array = [ 'foo' => [ 'bar' => [0, 1, 2, 'a'] ] ]; ArrayHelper::has($array, 'foo.bar.1') // true
Ids
此方法将返回输入数组中所有的 id 值。这是 ArrayHelper::column($array, 'id') 的别名;
ArrayHelper::ids($array)
演示
$array = [ [ 'id' => 1, 'name' => 'test1' ], [ 'id' => 2, 'name' => 'test2' ] ]; ArrayHelper::ids($array); result: [1, 2]
isAssoc
确定数组是否为关联数组
ArrayHelper::isAssoc(array $array)
演示
ArrayHelper::isAssoc([1, 2, 3]); result: false ArrayHelper::isAssoc(['foo' => 'bar']); result: true
isMulti
检查数组是否是多维的
ArrayHelper::isMulti(array $array, $strongCheck = false)
$array = [ ['foo' => 'bar'], ['foo' => 'bar'] ]; ArrayHelper::isMulti($array);
键值
将多维数组转换为键值数组
ArrayHelper::keyValue(array $items, $keyField = 'key', $valueField = 'value')
演示
$array = [ [ 'key' => 'name', 'value' => 'Dmitry' ], [ 'key' => 'country', 'value' => 'Russia' ], [ 'key' => 'city', 'value' => 'Oryol (eagle)' ] ]; $array = ArrayHelper::keyValue($array, 'key', 'value'); result: [ 'name' => 'Dmitry', 'country' => 'Russia', 'city' => 'Oryol (eagle)' ];
最后一个键
获取给定数组的最后一个键
ArrayHelper::lastKey($array)
演示
$array = [ 'a' => 1, 'b' => 2, 'c' => 3 ]; ArrayHelper::lastKey($array); // result: 'c'
映射
将回调应用于给定数组的元素
ArrayHelper::map($array, \Closure $callback)
演示
ArrayHelper::map($array, function($item) { return $item; });
仅
从给定数组中获取项目子集
ArrayHelper::only(array $array, array $keys)
演示
ArrayHelper::only(['a', 'b', 'c'], ['a', 'b']); result: ['a', 'b'];
使用关联数组
$array = [ 'foo' => 'bar', 'foo2' => 'bar2', 'foo3' => 'bar3' ]; ArrayHelper::only($array, ['foo2']); result: [ 'foo2' => 'bar2' ]
使用多数组
$array = [ [ 'foo' => 'bar', 'foo2' => 'bar2' ], [ 'foo' => 'bar', 'foo2' => 'bar2' ] ]; ArrayHelper::only($array, ['foo']); result: [ ['foo' => 'bar'], ['foo' => 'bar'] ]
使用关联数组
$array = [ 'foo' => 'bar', 'foo2' => 'bar2' ]; ArrayHelper::except($array, ['foo2']); result: ['foo' => 'bar']
使用多数组
$array = [ [ 'foo' => 'bar', 'foo2' => 'bar2' ], [ 'foo' => 'bar', 'foo2' => 'bar2' ] ]; ArrayHelper::except($array, ['foo2']); result: [ ['foo' => 'bar'], ['foo' => 'bar'] ]
仅使用键
从给定数组中获取带有键 $key 的项目子集
ArrayHelper::onlyWithKey(array $array, $key)
演示
$array = [ [ 'a' => 1, 'b' => 2 ], [ 'a' => 1, 'b' => 2 ], [ 'b' => 2 ] ]; ArrayHelper::onlyWithKey($array, 'a'); // result: [ [ 'a' => 1, 'b' => 2 ], [ 'a' => 1, 'b' => 2 ] ]
分页
提取数组的切片
ArrayHelper::paginate(array $array, $page, $limit)
演示
$array = [1, 2, 3, 4, 5, 6]; ArrayHelper::paginate($array, 1, 3) result: [1, 2, 3]
prepend
此方法将项目推送到数组的开头。
ArrayHelper::prepend(&$array, $keyOrValue, $value = null)
演示
$array = [ 1, 2, 3, 4 ]; ArrayHelper::prepend($array, -1); result: [-1, 1, 2, 3, 4] $array = [ 'foo' => 'bar' ]; ArrayHelper::prepend($array, 'test', 123); result: [ 'test' => 123, 'foo' => 'bar' ];
随机
从数组中随机选择一个或多个元素
ArrayHelper::random(array $array, $count = 1)
演示
ArrayHelper::random([1, 2, 3]) result: 1 or 2 or 3 ArrayHelper::random([1, 2, 3], 2); result: [1, 3]
重新索引
重新索引数组的所有键
ArrayHelper::reindex($array)
演示
$array = [ 1 => 10, 2 => 20 ]; ArrayHelper::reindex($array); result: [10, 20]
删除
使用点表示法从数组中删除给定的键(或键)
ArrayHelper::remove(array &$array, $keys)
演示
简单示例 1
$array = [ 'foo' => [ 'bar' => 'baz' ], 'foo1' => 123 ]; ArrayHelper::remove($array, 'foo.bar'); // result: [ 'foo' => [], 'foo1' => 123 ]
简单示例 2
$array = [ [ 'foo' => 'bar', 'test' => 'test1' ], [ 'foo' => 'bar', 'test' => 'test2' ] ]; ArrayHelper::remove($array, 'foo'); // result: [ ['test' => 'test1'], ['test' => 'test2'] ]
高级示例
$array = [ [ 'foo' => [ 'bar' => [ 'baz' => 1 ] ], 'test' => 'test', 'test2' => '123', 'only' => true ], [ 'foo' => [ 'bar' => [ 'baz' => 2 ] ], 'test' => 'test', 'test2' => 123 ] ]; ArrayHelper::remove($array, ['foo.bar.baz', 'test', 'only']); // result: [ [ 'foo' => [ 'bar' => [] ], 'test2' => '123' ], [ 'foo' => [ 'bar' => [] ], 'test2' => 123 ] ]
替换键
替换数组中的键。
ArrayHelper::replaceKey($oldKey, $newKey, &$array)
演示
$array = [ 'foo' => 'bar' ]; ArrayHelper::replaceKey('foo', 'baz', $array); // result ($array): [ 'baz' => 'bar' ]
设置
使用 "点" 表示法将值设置到数组中
ArrayHelper::set(array &$array, $key, $value)
演示
$array = [ 'product' => [ 'name' => 'Some name', 'price' => 500 ] ]; ArrayHelper::set($array, 'product.price', 600);
洗牌
ArrayHelper::shuffle(array $array)
演示
ArrayHelper::shuffle([1, 2, 3]); result: [3, 1, 2]
排序
这些方法对数组进行排序
ArrayHelper::sortBy(array $array, $key = null, $direction = 'ASC'); ArrayHelper::sortByAsc(array $array, $key = null); ArrayHelper::sortByDesc(array $array, $key = null);
演示
$array = [ ['id' => 1], ['id' => 10] ]; $result = ArrayHelper::sortByDesc($array, 'id'); result: [ ['id' => 10], ['id' => 1] ];
分割字符串
将给定的字符串分割为数组
ArrayHelper::splitString($str)
演示
$string = 'Ab Cd'; ArrayHelper::splitString($string); result: ['A', 'b', ' ', 'C', 'd']
总和
计算具有特定键的数组中值的总和
ArrayHelper::sum(array $array, $key)
$array = [ [ 'name' => 'entity1', 'total' => 5 ], [ 'name' => 'entity2', 'total' => 6 ] ]; $result = ArrayHelper::sum($array, 'total'); // result: 11
转换为数组
递归地将混合数据转换为数组
演示
ArrayHelper::toArray([stdClassInstance, stdClassInstance, someMixedClass]);
ArrayHelper::toArray('{"foo":{"bar":123}}');
疯狂示例
class SimpleClass { public $test = null; public function __construct() { $std = new \stdClass(); $std->f = (new \stdClass()); $std->f->b = [1, 2]; $this->test = [$std, ['a', 'b']]; } } class SimpleIteratorTestClass implements \Iterator { // ... private $array = []; public function __construct() { $this->position = 0; $std = new \stdClass(); $std->f = (new \stdClass()); $std->f->b = [1, 2]; $this->array = [1, 2, 3, 4, 'asd', $std, new SimpleClass(), ['yeah' => [1, 2]]]; } // ... } ArrayHelper::toArray(new SimpleIteratorTestClass()); result: [ 1, 2, 3, 4, 'asd', [ 'f' => [ 'b' => [1, 2] ] ], [ 'test' => [ [ 'f' => [ 'b' => [1, 2] ] ], [ 'a', 'b' ] ] ], [ 'yeah' => [1, 2] ] ]
toInt
将某些属性转换为int
ArrayHelper::toInt(array $source, array $keys = [])
演示
$source = [ "id" => "100", "created_at" => "10000", "name" => "Foo Bar" ]; $source = ArrayHelper::toInt($source, ["id" "created_at"]); // result: [ "id" => 100, "created_at" => 10000, "name" => "Foo Bar" ] // Convert all values: $source = ArrayHelper::toInt($source); // Since 1.1.0 $source = [ [ 'id' => '100', 'created_at' => '1000', 'name' => 'FooBar', 'other' => '200' ], [ 'id' => '200', 'created_at' => '2000', 'name' => 'FooBar', 'other' => '300' ] ]; ArrayHelper::toInt($source, ['id', 'created_at', 'other']);
唯一
从数组中删除重复值
ArrayHelper::unique(array $array, $key = null)
演示
$array = [ 'a', 'a', 'b', 'b' ]; $array = ArrayHelper::unique($array); // result: ['a', 'b']
$array = [ ['a', 'a', 'b', 'b'], ['a', 'a', 'b', 'b'] ]; $array = ArrayHelper::unique($array); // result: [ ['a', 'b'], ['a', 'b'] ]
$array = [ [ 'id' => 100, 'name' => 'Product 1' ], [ 'id' => 200, 'name' => 'Product 2' ], [ 'id' => 100, 'name' => 'Product 3' ], [ 'name' => 'Product 4' ] ]; $array = ArrayHelper::unique($array, 'id'); // result: [ [ 'id' => 100, 'name' => 'Product 1' ], [ 'id' => 200, 'name' => 'Product 2' ] ]
值
将多维数组展平为单个(扁平)数组
ArrayHelper::values(array $array)
演示
$array = [ 'name' => 'Dmitry R', 'country' => 'Russia', 'skills' => ['PHP', 'JS'], [ 'identifier' => 'vodka medved balalayka' ] ]; ArrayHelper::values($array); // result: ['Dmitry R', 'Russia', 'PHP', 'JS', 'vodka medved balalayka'];
包装
将值包装为数组
ArrayHelper::wrap($value)
演示
ArrayHelper::wrap('123'); // result: ['123']