PHPackages                             marsapp/arrayhelper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. marsapp/arrayhelper

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

marsapp/arrayhelper
===================

ArrayHelper liberary, providing functions such as rebuilding indexes, grouping, getting content, recursive difference sets, recursive sorting, etc.

0.3.4(6y ago)21.3k1[1 issues](https://github.com/marshung24/ArrayHelper/issues)2MITPHPPHP ^7

Since Mar 27Pushed 6y agoCompare

[ Source](https://github.com/marshung24/ArrayHelper)[ Packagist](https://packagist.org/packages/marsapp/arrayhelper)[ Docs](https://github.com/marshung24/ArrayHelper)[ RSS](/packages/marsapp-arrayhelper/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (1)Versions (7)Used By (2)

ArrayHelper
===========

[](#arrayhelper)

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](https://camo.githubusercontent.com/928885f4f5f95eba381056bfe423eddeb0f231416f217399cb7c125cc6ca2d10/68747470733a2f2f706f7365722e707567782e6f72672f6d6172736170702f617272617968656c7065722f762f737461626c65)](https://packagist.org/packages/marsapp/arrayhelper) [![Total Downloads](https://camo.githubusercontent.com/9a96ca53fc1739c38d0a1343dc7b86c14b3e53937ebfd74c3168be36fc92f403/68747470733a2f2f706f7365722e707567782e6f72672f6d6172736170702f617272617968656c7065722f646f776e6c6f616473)](https://packagist.org/packages/marsapp/arrayhelper) [![Latest Unstable Version](https://camo.githubusercontent.com/92c846d6e37bd9d817325283d4a591f65dc8832b93d7272ae753c576ea0619e9/68747470733a2f2f706f7365722e707567782e6f72672f6d6172736170702f617272617968656c7065722f762f756e737461626c65)](https://packagist.org/packages/marsapp/arrayhelper) [![License](https://camo.githubusercontent.com/09a6e8963437109ed77d28981a8f2d5af451d248f962dcf6485eaa8b57f2e223/68747470733a2f2f706f7365722e707567782e6f72672f6d6172736170702f617272617968656c7065722f6c6963656e7365)](https://packagist.org/packages/marsapp/arrayhelper)

Outline
=======

[](#outline)

- [Installation](#installation)
- [Usage](#usage)
    - [Example](#example)
- [API Reference](#api-reference)
    - [indexBy()](#indexby)
    - [groupBy()](#groupby)
    - [indexOnly()](#indexonly)
    - [getContent()](#getcontent)
    - [getFallContent()](#getfallcontent)
    - [gather()](#gather)
    - [diffRecursive()](#diffrecursive)
    - [sortRecursive()](#sortrecursive)
    - [filterKey()](#filterkey)

[Installation](#Outline)
========================

[](#installation)

Composer Install
----------------

[](#composer-install)

```
# composer require marsapp/arrayhelper

```

Include
-------

[](#include)

Include composer autoloader before use.

```
require __PATH__ . "vendor/autoload.php";
```

[Usage](#Outline)
=================

[](#usage)

[Example](#Outline)
-------------------

[](#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](#outline)
=========================

[](#api-reference)

[indexBy()](#outline)
---------------------

[](#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()](#outline)
---------------------

[](#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()](#outline)
-----------------------

[](#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()](#outline)
------------------------

[](#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()](#outline)
----------------------------

[](#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()](#outline)
--------------------

[](#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()](#outline)
---------------------------

[](#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()](#outline)
---------------------------

[](#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()](#outline)
-----------------------

[](#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'];
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~102 days

Total

5

Last Release

2196d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/34496839?v=4)[MarsHung](/maintainers/marshung24)[@marshung24](https://github.com/marshung24)

---

Top Contributors

[![marshung24](https://avatars.githubusercontent.com/u/34496839?v=4)](https://github.com/marshung24 "marshung24 (16 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/marsapp-arrayhelper/health.svg)

```
[![Health](https://phpackages.com/badges/marsapp-arrayhelper/health.svg)](https://phpackages.com/packages/marsapp-arrayhelper)
```

###  Alternatives

[jawira/case-converter

Convert strings between 13 naming conventions: Snake case, Camel case, Pascal case, Kebab case, Ada case, Train case, Cobol case, Macro case, Upper case, Lower case, Sentence case, Title case and Dot notation.

1746.9M79](/packages/jawira-case-converter)[awcodes/drop-in-action

A form component for Filamentphp allowing actions inside of forms.

8656.0k](/packages/awcodes-drop-in-action)[hubertusanton/silverstripe-seo

SEO module for Silverstripe

4823.0k2](/packages/hubertusanton-silverstripe-seo)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
