petrgrishin / array-map
PHP中使用数组面向对象的解决方案
1.4.0
2018-06-22 15:01 UTC
Requires
- php: >=5.3.0
- petrgrishin/array-object: ~1.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-20 20:05:52 UTC
README
数组操作面向对象的方法
安装
将依赖项添加到项目的 composer.json 文件中
{ "require": { "petrgrishin/array-map": "~1.0" } }
使用示例
映射
使用键
$array = ArrayMap::create($array) ->map(function ($value, $key) { return array($key => $value); }) ->getArray();
简单
$array = ArrayMap::create($array) ->map(function ($value) { return $value; }) ->getArray();
合并
递归合并
$array = ArrayMap::create($array) ->mergeWith(array( 1 => 1, 2 => 2, 3 => array( 1 => 1, 2 => 2, ), )) ->getArray();
单级合并
$array = ArrayMap::create($array) ->mergeWith(array( 1 => 1, 2 => 2, ), false) ->getArray();
过滤
$array = ArrayMap::create($array) ->filter(function ($value, $key) { return $value > 10 && $key > 2; }) ->getArray();
用户排序
按值排序
$array = ArrayMap::create($array) ->userSortByValue(function ($first, $second) { return $first < $second ? -1 : 1; }) ->getArray();
按键排序
$array = ArrayMap::create($array) ->userSortByKey(function ($first, $second) { return $first < $second ? -1 : 1; }) ->getArray();
使用示例
ArrayAccess 类,多数组访问 — https://github.com/petrgrishin/array-access