PHPackages                             nick-lai/associative-array - 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. nick-lai/associative-array

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

nick-lai/associative-array
==========================

A lightweight associative array library for PHP.

v2.3.1(5mo ago)6382MITPHPPHP ^7.0|^8.0CI failing

Since Jun 28Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/nick-lai/associative-array)[ Packagist](https://packagist.org/packages/nick-lai/associative-array)[ Docs](https://github.com/nick-lai/associative-array)[ RSS](/packages/nick-lai-associative-array/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (10)Dependencies (1)Versions (13)Used By (0)

AssociativeArray
================

[](#associativearray)

[![Tests](https://github.com/nick-lai/associative-array/actions/workflows/tests.yml/badge.svg)](https://github.com/nick-lai/associative-array/actions/workflows/tests.yml)[![codecov](https://camo.githubusercontent.com/719cccf06d5b18aa37ede6613c025e3e7f7600b9fedc52072fc7a66faaed729f/68747470733a2f2f636f6465636f762e696f2f67682f6e69636b2d6c61692f6173736f636961746976652d61727261792f67726170682f62616467652e7376673f746f6b656e3d59734953385345476f78)](https://codecov.io/gh/nick-lai/associative-array)[![Maintainability](https://camo.githubusercontent.com/2d65ddd32e2b940ce1dc912c5e9b10a53a62cda90d78d6caca731be369c2e01d/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f36313963656638326433656261326561373335632f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/nick-lai/associative-array/maintainability)[![Packagist](https://camo.githubusercontent.com/964be11253baafc90b72fb9ec08f2886ed06786e243a1296affc659e99abf57c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e69636b2d6c61692f6173736f636961746976652d61727261792e737667)](https://packagist.org/packages/nick-lai/associative-array)[![Total Downloads](https://camo.githubusercontent.com/cec0ea7871998736b23af68abb439ba6a78405eecbf230bda93ad824cd371989/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e69636b2d6c61692f6173736f636961746976652d61727261792e7376673f636f6c6f723d627269676874677265656e)](https://packagist.org/packages/nick-lai/associative-array)

**A lightweight associative array library for PHP.**

Table of Contents
-----------------

[](#table-of-contents)

- [AssociativeArray](#associativearray)
    - [Table of Contents](#table-of-contents)
    - [Requirements](#requirements)
    - [Installation](#installation)
    - [Basic Usage](#basic-usage)
        - [select()](#select)
        - [where()](#where)
        - [innerJoin()](#innerjoin)
        - [leftJoin()](#leftjoin)
        - [rightJoin()](#rightjoin)
        - [orderBy()](#orderby)
        - [groupBy()](#groupby)
        - [make()](#make)
        - [first()](#first)
        - [last()](#last)
        - [count()](#count)
        - [sum()](#sum)
        - [avg()](#avg)
        - [toArray()](#toarray)
        - [Array Access](#array-access)
        - [Traversable](#traversable)
    - [License](#license)

Requirements
------------

[](#requirements)

AssociativeArray requires PHP &gt;= 7.0.0

Installation
------------

[](#installation)

```
composer require nick-lai/associative-array
```

Basic Usage
-----------

[](#basic-usage)

```
use NickLai\AssociativeArray;

$data = [
    ['id' => 1001, 'category' => 'C', 'price' => 20],
    ['id' => 1002, 'category' => 'B', 'price' => 15],
    ['id' => 1003, 'category' => 'A', 'price' => 15],
    ['id' => 1004, 'category' => 'A', 'price' => 25],
    ['id' => 1005, 'category' => 'B', 'price' => 10],
];

$associativeArray = new AssociativeArray($data);

var_export([
    'first' => $associativeArray->first(),
    'last' => $associativeArray->last(),
    'count' => $associativeArray->count(),
    'sum(price)' => $associativeArray->sum('price'),
    'avg(price)' => $associativeArray->avg('price'),
]);
```

Result:

```
array (
  'first' =>
  array (
    'id' => 1001,
    'category' => 'C',
    'price' => 20,
  ),
  'last' =>
  array (
    'id' => 1005,
    'category' => 'B',
    'price' => 10,
  ),
  'count' => 5,
  'sum(price)' => 85,
  'avg(price)' => 17,
)

```

### select()

[](#select)

Get rows of selected columns.

```
$associativeArray = new AssociativeArray([
    ['id' => 1001, 'category' => 'C', 'price' => 30],
    ['id' => 1002, 'category' => 'A', 'price' => 25],
    ['id' => 1003, 'category' => 'B', 'price' => 10],
]);

var_export($associativeArray->select(['id', 'price'])->toArray());
var_export($associativeArray->select(['category', 'price'])->toArray());
```

Result:

```
array (
  0 =>
  array (
    'id' => 1001,
    'price' => 30,
  ),
  1 =>
  array (
    'id' => 1002,
    'price' => 25,
  ),
  2 =>
  array (
    'id' => 1003,
    'price' => 10,
  ),
)

array (
  0 =>
  array (
    'category' => 'C',
    'price' => 30,
  ),
  1 =>
  array (
    'category' => 'A',
    'price' => 25,
  ),
  2 =>
  array (
    'category' => 'B',
    'price' => 10,
  ),
)

```

### where()

[](#where)

Filter the rows using the given callback.

```
$associativeArray = new AssociativeArray([
    ['id' => 1001, 'category' => 'C', 'price' => 30],
    ['id' => 1002, 'category' => 'A', 'price' => 25],
    ['id' => 1003, 'category' => 'B', 'price' => 10],
]);

$result = $associativeArray->where(function ($row) {
    return $row['price'] > 10;
})->toArray();

var_export($result);
```

Result:

```
array (
  0 =>
  array (
    'id' => 1001,
    'category' => 'C',
    'price' => 30,
  ),
  1 =>
  array (
    'id' => 1002,
    'category' => 'A',
    'price' => 25,
  ),
)

```

### innerJoin()

[](#innerjoin)

Inner join rows

```
$associativeArray = new AssociativeArray([
    1001 => ['id' => 1001, 'category' => 'C', 'price' => 30],
    1002 => ['id' => 1002, 'category' => 'A', 'price' => 25],
    1003 => ['id' => 1003, 'category' => 'B', 'price' => 10],
    1004 => ['id' => 1004, 'category' => 'X', 'price' => 60],
]);

$categories = [
    'A' => ['category' => 'A', 'desc' => 'A desc'],
    'B' => ['category' => 'B', 'desc' => 'B desc'],
    'C' => ['category' => 'C', 'desc' => 'C desc'],
    'D' => ['category' => 'D', 'desc' => 'D desc'],
];

$result = $associativeArray->innerJoin($categories, function ($leftRow, $rightRow) {
    return $leftRow['category'] === $rightRow['category'];
})->toArray();

var_export($result);
```

Result:

```
array (
  1001 =>
  array (
    'id' => 1001,
    'category' => 'C',
    'price' => 30,
    'desc' => 'C desc',
  ),
  1002 =>
  array (
    'id' => 1002,
    'category' => 'A',
    'price' => 25,
    'desc' => 'A desc',
  ),
  1003 =>
  array (
    'id' => 1003,
    'category' => 'B',
    'price' => 10,
    'desc' => 'B desc',
  ),
)

```

### leftJoin()

[](#leftjoin)

Left join rows

```
$associativeArray = new AssociativeArray([
    1001 => ['id' => 1001, 'category' => 'C', 'price' => 30],
    1002 => ['id' => 1002, 'category' => 'A', 'price' => 25],
    1003 => ['id' => 1003, 'category' => 'B', 'price' => 10],
    1004 => ['id' => 1004, 'category' => 'X', 'price' => 60],
]);

$categories = [
    'A' => ['category' => 'A', 'desc' => 'A desc'],
    'B' => ['category' => 'B', 'desc' => 'B desc'],
    'C' => ['category' => 'C', 'desc' => 'C desc'],
    'D' => ['category' => 'D', 'desc' => 'D desc'],
];

$result = $associativeArray->leftJoin($categories, function ($leftRow, $rightRow) {
    return $leftRow['category'] === $rightRow['category'];
})->toArray();

var_export($result);
```

Result:

```
array (
  1001 =>
  array (
    'id' => 1001,
    'category' => 'C',
    'price' => 30,
    'desc' => 'C desc',
  ),
  1002 =>
  array (
    'id' => 1002,
    'category' => 'A',
    'price' => 25,
    'desc' => 'A desc',
  ),
  1003 =>
  array (
    'id' => 1003,
    'category' => 'B',
    'price' => 10,
    'desc' => 'B desc',
  ),
  1004 =>
  array (
    'id' => 1004,
    'category' => 'X',
    'price' => 60,
    'desc' => NULL,
  ),
)

```

### rightJoin()

[](#rightjoin)

Right join rows

```
$associativeArray = new AssociativeArray([
    1001 => ['id' => 1001, 'category' => 'C', 'price' => 30],
    1002 => ['id' => 1002, 'category' => 'A', 'price' => 25],
    1003 => ['id' => 1003, 'category' => 'B', 'price' => 10],
    1004 => ['id' => 1004, 'category' => 'X', 'price' => 60],
]);

$categories = [
    'A' => ['category' => 'A', 'desc' => 'A desc'],
    'B' => ['category' => 'B', 'desc' => 'B desc'],
    'C' => ['category' => 'C', 'desc' => 'C desc'],
    'D' => ['category' => 'D', 'desc' => 'D desc'],
];

$result = $associativeArray->rightJoin($categories, function ($leftRow, $rightRow) {
    return $leftRow['category'] === $rightRow['category'];
})->toArray();

var_export($result);
```

Result:

```
array (
  'A' =>
  array (
    'category' => 'A',
    'desc' => 'A desc',
    'id' => 1002,
    'price' => 25,
  ),
  'B' =>
  array (
    'category' => 'B',
    'desc' => 'B desc',
    'id' => 1003,
    'price' => 10,
  ),
  'C' =>
  array (
    'category' => 'C',
    'desc' => 'C desc',
    'id' => 1001,
    'price' => 30,
  ),
  'D' =>
  array (
    'category' => 'D',
    'desc' => 'D desc',
    'id' => NULL,
    'price' => NULL,
  ),
)

```

### orderBy()

[](#orderby)

Order by keys

```
$associativeArray = new AssociativeArray([
    ['id' => 1001, 'category' => 'C', 'price' => 10],
    ['id' => 1002, 'category' => 'A', 'price' => 25],
    ['id' => 1003, 'category' => 'B', 'price' => 10],
]);

$result = $associativeArray->orderBy(['price', 'category'], ['desc', 'asc'])->toArray();

var_export($result);
```

Result:

```
array (
  0 =>
  array (
    'id' => 1002,
    'category' => 'A',
    'price' => 25,
  ),
  1 =>
  array (
    'id' => 1003,
    'category' => 'B',
    'price' => 10,
  ),
  2 =>
  array (
    'id' => 1001,
    'category' => 'C',
    'price' => 10,
  ),
)

```

Keep index

```
$associativeArray = new AssociativeArray([
    'X' => ['id' => 1001, 'category' => 'C', 'price' => 10],
    'Y' => ['id' => 1002, 'category' => 'A', 'price' => 25],
    'Z' => ['id' => 1003, 'category' => 'B', 'price' => 10],
]);

$result = $associativeArray->orderBy('category', 'asc', true)->toArray();

var_export($result);
```

Result:

```
array (
  'Y' =>
  array (
    'id' => 1002,
    'category' => 'A',
    'price' => 25,
  ),
  'Z' =>
  array (
    'id' => 1003,
    'category' => 'B',
    'price' => 10,
  ),
  'X' =>
  array (
    'id' => 1001,
    'category' => 'C',
    'price' => 10,
  ),
)
```

### groupBy()

[](#groupby)

Groups an associative array by keys.

```
$associativeArray = new AssociativeArray([
    ['id' => 1001, 'category' => 'B', 'price' => 30],
    ['id' => 1002, 'category' => 'A', 'price' => 25],
    ['id' => 1003, 'category' => 'B', 'price' => 30],
    ['id' => 1004, 'category' => 'A', 'price' => 30],
]);

$result = $associativeArray->groupBy(['category', 'price']);

var_export($result);
```

Result:

```
array (
  'B' =>
  array (
    30 =>
    array (
      0 =>
      array (
        'id' => 1001,
        'category' => 'B',
        'price' => 30,
      ),
      1 =>
      array (
        'id' => 1003,
        'category' => 'B',
        'price' => 30,
      ),
    ),
  ),
  'A' =>
  array (
    25 =>
    array (
      0 =>
      array (
        'id' => 1002,
        'category' => 'A',
        'price' => 25,
      ),
    ),
    30 =>
    array (
      0 =>
      array (
        'id' => 1004,
        'category' => 'A',
        'price' => 30,
      ),
    ),
  ),
)

```

### make()

[](#make)

Create a new associative array instance.

```
$data = [
    ['id' => 1001, 'category' => 'C', 'price' => 10],
    ['id' => 1002, 'category' => 'A', 'price' => 25],
    ['id' => 1003, 'category' => 'B', 'price' => 10],
];

var_export(AssociativeArray::make($data)->first());
```

Result:

```
array (
  'id' => 1001,
  'category' => 'C',
  'price' => 10,
)

```

### first()

[](#first)

Return the first row

```
$associativeArray = new AssociativeArray([
    ['id' => 1001, 'category' => 'C', 'price' => 10],
    ['id' => 1002, 'category' => 'A', 'price' => 25],
    ['id' => 1003, 'category' => 'B', 'price' => 10],
]);

var_export($associativeArray->first());
```

Result:

```
array (
  'id' => 1001,
  'category' => 'C',
  'price' => 10,
)

```

### last()

[](#last)

Return the last row

```
$associativeArray = new AssociativeArray([
    ['id' => 1001, 'category' => 'C', 'price' => 10],
    ['id' => 1002, 'category' => 'A', 'price' => 25],
    ['id' => 1003, 'category' => 'B', 'price' => 10],
]);

var_export($associativeArray->last());
```

Result:

```
array (
  'id' => 1003,
  'category' => 'B',
  'price' => 10,
)

```

### count()

[](#count)

Count the number of rows in the associative array.

```
$associativeArray = new AssociativeArray([
    ['id' => 1001, 'category' => 'C', 'price' => 10],
    ['id' => 1002, 'category' => 'A', 'price' => 25],
    ['id' => 1003, 'category' => 'B', 'price' => 10],
]);

var_export($associativeArray->count());
```

Result:

```
3

```

### sum()

[](#sum)

Get the sum of a given key.

```
$associativeArray = new AssociativeArray([
    ['id' => 1001, 'category' => 'C', 'price' => 10],
    ['id' => 1002, 'category' => 'A', 'price' => 25],
    ['id' => 1003, 'category' => 'B', 'price' => 10],
]);

var_export($associativeArray->sum('price'));
```

Result:

```
45

```

### avg()

[](#avg)

Get the average value of a given key.

```
$associativeArray = new AssociativeArray([
    ['id' => 1001, 'category' => 'C', 'price' => 10],
    ['id' => 1002, 'category' => 'A', 'price' => 25],
    ['id' => 1003, 'category' => 'B', 'price' => 10],
]);

var_export($associativeArray->avg('price'));
```

Result:

```
15

```

### toArray()

[](#toarray)

Get the instance as an array.

```
$associativeArray = new AssociativeArray([
    ['id' => 1001, 'category' => 'C', 'price' => 10],
    ['id' => 1002, 'category' => 'A', 'price' => 25],
    ['id' => 1003, 'category' => 'B', 'price' => 10],
]);

var_export($associativeArray->toArray());
```

Result:

```
array (
  0 =>
  array (
    'id' => 1001,
    'category' => 'C',
    'price' => 10,
  ),
  1 =>
  array (
    'id' => 1002,
    'category' => 'A',
    'price' => 25,
  ),
  2 =>
  array (
    'id' => 1003,
    'category' => 'B',
    'price' => 10,
  ),
)

```

### Array Access

[](#array-access)

```
$associativeArray = new AssociativeArray([
    ['id' => 1001, 'category' => 'C', 'price' => 10],
    ['id' => 1002, 'category' => 'A', 'price' => 25],
    ['id' => 1003, 'category' => 'B', 'price' => 10],
]);

var_export($associativeArray[0]);
```

Result:

```
array (
  'id' => 1001,
  'category' => 'C',
  'price' => 10,
)

```

### Traversable

[](#traversable)

```
$associativeArray = new AssociativeArray([
    ['id' => 1001, 'category' => 'C', 'price' => 10],
    ['id' => 1002, 'category' => 'A', 'price' => 25],
    ['id' => 1003, 'category' => 'B', 'price' => 10],
]);

foreach ($associativeArray as $row) {
    echo $row['category'] . PHP_EOL;
}
```

Result:

```
C
A
B

```

License
-------

[](#license)

AssociativeArray is released under the MIT Licence. See the bundled LICENSE file for details.

###  Health Score

45

—

FairBetter than 92% of packages

Maintenance70

Regular maintenance activity

Popularity16

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 97.1% 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 ~212 days

Recently: every ~581 days

Total

12

Last Release

171d ago

Major Versions

v1.1.2 → v2.0.02019-07-15

PHP version history (2 changes)v1.0.0PHP ^7.0

v2.3.0PHP ^7.0|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/10176232c981785d06e3ea493f7c5666ebaf4d75e2c251be3f0a371a4e8f317c?d=identicon)[nick-lai](/maintainers/nick-lai)

---

Top Contributors

[![nick-lai](https://avatars.githubusercontent.com/u/7104395?v=4)](https://github.com/nick-lai "nick-lai (34 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

associative-arraycomposer-packagephparrayAssociativeassociative array

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nick-lai-associative-array/health.svg)

```
[![Health](https://phpackages.com/badges/nick-lai-associative-array/health.svg)](https://phpackages.com/packages/nick-lai-associative-array)
```

###  Alternatives

[doctrine/collections

PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.

6.0k411.1M1.2k](/packages/doctrine-collections)[symfony/property-access

Provides functions to read and write from/to an object or array using a simple string notation

2.8k295.3M2.5k](/packages/symfony-property-access)[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k394.3M1.5k](/packages/nette-utils)[league/config

Define configuration arrays with strict schemas and access values with dot notation

564302.2M24](/packages/league-config)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k9.2M108](/packages/cuyz-valinor)[openlss/lib-array2xml

Array2XML conversion library credit to lalit.org

31052.5M47](/packages/openlss-lib-array2xml)

PHPackages © 2026

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