marsapp/arrayhelper
Composer 安装命令:
composer require marsapp/arrayhelper
包简介
ArrayHelper liberary, providing functions such as rebuilding indexes, grouping, getting content, recursive difference sets, recursive sorting, etc.
README 文档
README
Array processing library, providing functions such as rebuilding indexes, grouping, getting content, recursive difference sets, recursive sorting, etc.
Continuation library marshung/helper, only keep and maintain ArrayHelper
Outline
Installation
Composer Install
# composer require marsapp/arrayhelper
Include
Include composer autoloader before use.
require __PATH__ . "vendor/autoload.php";
Usage
Example
Namespace use:
// Use namespace use marsapp\helper\myarray\ArrayHelper; // Data $data = [ ['c_sn' => 'a110', 'u_sn' => 'b1', 'u_no' => 'a001', 'u_name' => 'name1'], ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'name2'], ]; // Index by ArrayHelper::indexBy($data, ['c_sn', 'u_no']); // Get name by a110 => a001 => u_name $name = ArrayHelper::getContent($data, 'a110, a001, u_name'); // $name = name1;
API Reference
indexBy()
Data re-index by keys
indexBy(Array & $data, Array|String $keys, Bool $obj2array = false) : array
Since $data is a reference, $data will change after indexBy() is executed.
Since $data is a reference, the return is useless.
If you want to keep $data, you can clone it before using it.
Example :
$data = [ ['c_sn' => 'a110', 'u_sn' => 'b1', 'u_no' => 'a001', 'u_name' => 'name1'], ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'name2'], ]; ArrayHelper::indexBy($data, ['c_sn','u_sn','u_no']); // $data = [ // 'a110' => [ // 'b1' => ['a001' => ['c_sn' => 'a110', 'u_sn' => 'b1', 'u_no' => 'a001', 'u_name' => 'name1']], // 'b2' => ['b012' => ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'name2']], // ], // ];
groupBy()
Data re-index and Group by keys
groupBy(Array & $data, Array|String $keys, Bool $obj2array = false) : array
Since $data is a reference, $data will change after indexBy() is executed.
Since $data is a reference, the return is useless.
If you want to keep $data, you can clone it before using it.
Example :
$data = [ ['c_sn' => 'a110', 'u_sn' => 'b1', 'u_no' => 'a001', 'u_name' => 'name1'], ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'name2'], ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'user name 3'], ]; ArrayHelper::groupBy($data, ['c_sn','u_sn','u_no']);
$data reqult:
[
'a110' => [
'b1' => ['a001' => [
0 => ['c_sn' => 'a110', 'u_sn' => 'b1', 'u_no' => 'a001', 'u_name' => 'name1']
]
],
'b2' => ['b012' => [
0 => ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'name2'],
1 => ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'user name 3']
]
],
],
];
indexOnly()
Data re-index by keys, No Data
indexOnly(Array & $data, Array|String $keys, Bool $obj2array = false) : array
Since $data is a reference, $data will change after indexBy() is executed.
Since $data is a reference, the return is useless.
If you want to keep $data, you can clone it before using it.
Example :
$data = [ ['c_sn' => 'a110', 'u_sn' => 'b1', 'u_no' => 'a001', 'u_name' => 'name1'], ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'name2'], ]; ArrayHelper::indexOnly($data, ['c_sn','u_sn','u_no']);
$data reqult:
[
'a110' => [
'b1' => [
'a001' => ''
],
'b2' => [
'b012' => ''
],
],
];
getContent()
Get Data content by index
getContent(Array $data, Array|String $indexTo = [], Bool $exception = false) : array|mixed
Example:
$data = ['user' => ['name' => 'Mars', 'birthday' => '2000-01-01']]; // No indexTo, get all $output = ArrayHelper::getContent($data); // $output: ['user' => ['name' => 'Mars', 'birthday' => '2000-01-01']]; // Target is array $output = ArrayHelper::getContent($data, 'user'); $output = ArrayHelper::getContent($data, ['user']); // $output: ['name' => 'Mars', 'birthday' => '2000-01-01']; // Target is string $output = ArrayHelper::getContent($data, 'user, name'); $output = ArrayHelper::getContent($data, ['user', 'name']); // $outpu: Mars // No target $output = ArrayHelper::getContent($data, 'user, name, aaa'); $output = ArrayHelper::getContent($data, ['user', 'name', 'aaa']); // $outpu: []
getFallContent()
Get fall point content
- Get the data in an ordered non-contiguous index array
- If there is no fall point, return null.
- Ensure performance by sorting $data ahead of time:
- a. Sorting $data (ASC)
- b. Close $sortOut
- c. Use function ArrayHelper::getFallContent()
getFallContent(Array $data, $referKey, $sortOut = 'default') : mixed
Parameters
- $data: The array to compare from. array
- $referKey: Refer key to compare against. string
- $sortOut: Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut()
Return Values
- Returns the resulting mixed.
Example :
$data = ['2019-05-01' => '20', '2019-06-01' => '30', '2019-06-15' => '50']; $value = ArrayHelper::getFallContent($data, '2019-06-11', false); // $value = 30;
gather()
Data gather by list
Collect and classify target data according to the list of fields
gather(Array $data, Array $colNameList, Int $objLv = 1) : array
Example Data :
$data = [ 0 => ['sn' => '1785','m_sn' => '40','d_sn' => '751','r_type' => 'staff','manager' => '1','s_manager' => '1','c_user' => '506'], 1 => ['sn' => '1371','m_sn' => '40','d_sn' => '583','r_type' => 'staff','manager' => '61','s_manager' => '0','c_user' => '118'], 2 => ['sn' => '1373','m_sn' => '40','d_sn' => '584','r_type' => 'staff','manager' => '61','s_manager' => '0','c_user' => '118'], 3 => ['sn' => '7855','m_sn' => '40','d_sn' => '2303','r_type' => 'staff','manager' => '71','s_manager' => '0','c_user' => '61'], 4 => ['sn' => '7856','m_sn' => '40','d_sn' => '2304','r_type' => 'staff','manager' => '75','s_manager' => '0','c_user' => '61'] ];
Example 1 :
Field
manager,s_manager,c_user valuesare placed in the same one-dimensional array
$ssnList1 = ArrayHelper::gather($data, array('manager', 's_manager','c_user'), 1);
$ssnList1 reqult:
[1 => '1',506 => '506',61 => '61',0 => '0',118 => '118',71 => '71',75 => '75'];
Example 2 :
The field
manageris placed in an array, the fieldss_manager, and thec_uservalues are placed in the same array. Form a 2-dimensional array
$ssnList2 = ArrayHelper::gather($data, array('manager' => array('manager'), 'other' => array('s_manager','c_user')), 1);
$ssnList2 reqult:
[
'manager' => [1 => '1',61 => '61',71 => '71',75 => '75'],
'other' => [1 => '1',506 => '506',0 => '0',118 => '118',61 => '61']
];
diffRecursive()
Array Deff Recursive
Compare $srcArray with $contrast and display it if something on $srcArray is not on $contrast.
diffRecursive(Array $srcArray, $contrast) : array
Example :
$data1 = [ 0 => ['c_sn' => 'a110', 'u_sn' => 'b1', 'u_no' => 'a001', 'u_name' => 'name1'], 1 => ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'name2'], 2 => ['c_sn' => 'a110', 'u_sn' => null, 'u_no' => 'c024', 'u_name' => 'name3'], ]; $data2 = [ 0 => ['c_sn' => 'a110', 'u_sn' => 'b1', 'u_no' => 'a001', 'u_name' => 'name1'], 1 => ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'name2222'], 2 => ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'c024', 'u_name' => 'user name 3'], ]; $diff = ArrayHelper::diffRecursive($data1, $data2);
$diff result :
[
1 => ['u_name' => 'name2'],
2 => ['u_sn' => NULL,'u_name' => 'name3']
];
sortRecursive()
Array Sort Recursive
sortRecursive(Array & $srcArray, $type = 'ksort') : void
$srcArray is a reference
$type : ksort(default), krsort, sort, rsort
Example :
$data1 = [ 'b1' => [ 0 => ['c_sn' => 'a110', 'u_sn' => 'b1', 'u_no' => 'a001', 'u_name' => 'name1'] ], 'b2' => [ 0 => ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'name2'], 1 => ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'user name 3'] ], ]; $data2 = $data1; ArrayHelper::sortRecursive($data1, 'ksort'); ArrayHelper::sortRecursive($data2, 'krsort');
$data1 result:
[
'b1' => [
0 => ['c_sn' => 'a110','u_name' => 'name1','u_no' => 'a001','u_sn' => 'b1']
],
'b2' => [
0 => ['c_sn' => 'a110','u_name' => 'name2','u_no' => 'b012','u_sn' => 'b2'],
1 => ['c_sn' => 'a110','u_name' => 'user name 3','u_no' => 'b012','u_sn' => 'b2']
]
];
$data2 result:
[
'b2' => [
1 => ['u_sn' => 'b2','u_no' => 'b012','u_name' => 'user name 3','c_sn' => 'a110'],
0 => ['u_sn' => 'b2','u_no' => 'b012','u_name' => 'name2','c_sn' => 'a110']
],
'b1' => [
0 => ['u_sn' => 'b1','u_no' => 'a001','u_name' => 'name1','c_sn' => 'a110']
]
];
filterKey()
Filter array according to the allowed keys
filterKey(Array $array, $keys, $fillKey = true) : array
Parameters
- $array: The array to compare from. array
- $keys: Key list to compare against. array|string
- $fillKey: Fill the key that does not exist in the array, default true. bool
Return Values
- Returns the resulting array.
Example :
$array = ['sn' => '1785','m_sn' => '40','d_sn' => '751','r_type' => 'staff','manager' => '1','s_manager' => '1','c_user' => '506']; // fill key $result = ArrayHelper::filterKey($array, ['sn', 'd_sn', 'r_type', 'manager', 'nooooooooo']); $result = ArrayHelper::filterKey($array, 'sn,d_sn, r_type, manager, nooooooooo'); // $result = ['sn' => '1785','d_sn' => '751','r_type' => 'staff','manager' => '1', 'nooooooooo' => '']; // No fill key $result = ArrayHelper::filterKey($array, ['sn', 'd_sn', 'r_type', 'manager', 'nooooooooo'], false); $result = ArrayHelper::filterKey($array, 'sn,d_sn, r_type, manager, nooooooooo', false); // $result = ['sn' => '1785','d_sn' => '751','r_type' => 'staff','manager' => '1'];
marsapp/arrayhelper 适用场景与选型建议
marsapp/arrayhelper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.33k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 03 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 marsapp/arrayhelper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 marsapp/arrayhelper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-26