定制 marsapp/arrayhelper 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

Latest Stable Version Total Downloads Latest Unstable Version License

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

  1. Get the data in an ordered non-contiguous index array
  2. If there is no fall point, return null.
  3. 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 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 fields s_manager, and the c_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']
];

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 marsapp/arrayhelper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.33k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-03-26