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
# composer require marsapp/arrayhelper
Include composer autoloader before use.
require __PATH__ . "vendor/autoload.php";
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;
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']],
// ],
// ];
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']
]
],
],
];
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' => ''
],
],
];
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: []
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;
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 values
are 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
manager
is placed in an array, the fieldss_manager
, and thec_user
values 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']
];
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']
];
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']
]
];
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'];