PHPackages                             thorough-php/arrays - 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. thorough-php/arrays

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

thorough-php/arrays
===================

This is a collection of array wrappers

v2.0(7y ago)2230MITPHPPHP &gt;=7.2

Since Oct 4Pushed 7y ago1 watchersCompare

[ Source](https://github.com/ThoroughPHP/Arrays)[ Packagist](https://packagist.org/packages/thorough-php/arrays)[ RSS](/packages/thorough-php-arrays/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

Array Extensions
================

[](#array-extensions)

[![Build Status](https://camo.githubusercontent.com/7ed0d1f85f755e4ac2c14a34368fd687e98f1959bf9a2113773861ef05f87a95/68747470733a2f2f7472617669732d63692e636f6d2f54686f726f7567685048502f4172726179732e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/ThoroughPHP/Arrays)[![Coverage Status](https://camo.githubusercontent.com/4e15a31da4fda689cb6d486b0aa7292211384d4be0c66fb527f811eda71eb275/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f54686f726f7567685048502f4172726179732f62616467652e737667)](https://coveralls.io/github/ThoroughPHP/Arrays)[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)[![PHPStan](https://camo.githubusercontent.com/441b5874ce4df0a2defc892979c96c46889b69cb32119d04f0b48626349f8bc9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d656e61626c65642d627269676874677265656e2e7376673f7374796c653d666c6174)](https://github.com/phpstan/phpstan)

This is a collection of array wrappers.

Table of Contents
=================

[](#table-of-contents)

- [Composite Key Array](#composite-key-array)
    - [1. offsetExists](#composite-key-array-offset-exists)
    - [2. offsetGet](#composite-key-array-offset-get)
    - [3. offsetSet](#composite-key-array-offset-set)
    - [4. offsetUnset](#composite-key-array-offset-unset)
- [XPath Key Array](#xpath-key-array)
- [Dotted Key Array](#dotted-key-array)
- [One-off Array](#one-off-array)
- [Write-once Array](#write-once-array)

Composite Key Array
-------------------

[](#composite-key-array)

Sometimes it is useful to have ability to access nested value with one, array like, key (some questions were asked about this: [on quora](https://www.quora.com/Learning-PHP-Is-there-a-way-to-get-the-value-of-multi-dimensional-array-by-specifying-the-key-with-a-variable), [on stackoverflow](http://stackoverflow.com/questions/22614817/get-a-value-from-a-multidimensional-array-using-the-dot-syntax)).

`CompositeKeyArray` class gives you the ability to do all basic array operations using array-like (nested) key. It implemets [`ArrayAccess`](http://php.net/manual/en/class.arrayaccess.php) interface:

### 1. offsetExists:

[](#1-offsetexists)

You can check nested keys for existance.

```
    $array = new CompositeKeyArray([
        'foo' => [
            'bar' => 'baz'
        ]
    ]);

    var_dump(isset($array[['foo', 'bar']])); // => bool(true)
    var_dump(isset($array[['foo', 'quux']])); // => bool(false)
```

### 2. offsetGet:

[](#2-offsetget)

You can get value by nested key. If nested key is not set the `UndefinedOffsetException` will be thrown.

```
    $array = new CompositeKeyArray([
        'foo' => [
            'bar' => 'baz'
        ]
    ]);

    var_dump($array[['foo', 'bar']]); // => string(3) "baz"
    var_dump($array[['foo', 'quux']]); // => PHP Fatal error:  Uncaught UndefinedOffsetException: Undefined offset quux.
```

### 3. offsetSet:

[](#3-offsetset)

You can set value for nested key.

```
    $array = new CompositeKeyArray();

    $array[['foo', 'bar']] = 'baz';

    var_dump($array[['foo', 'bar']]); // => string(3) "baz"
```

There is one pitfall. When you try to do `$array['foo']['bar'] = 'baz'` you get `Indirect modification of overloaded element of CompositeKeyArray has no effect`. The reason was explained [here](http://stackoverflow.com/questions/20053269/indirect-modification-of-overloaded-element-of-splfixedarray-has-no-effect). So in order to achive the desired result you have to do the following:

```
    $array = new CompositeKeyArray([
        'foo' => []
    ]);

    $array['foo']['bar'] = 'baz'; // => PHP Notice:  Indirect modification of overloaded element of CompositeKeyArray has no effect

    var_dump($array['foo']); // => array(0) {}

    $array[['foo', 'bar']] = 'baz';

    var_dump($array['foo']); // => array(1) {["bar"] => string(3) "baz"}
```

But there is another edge case left: when you need to append element at the end of an array.

```
    $array = new CompositeKeyArray([
        'foo' => []
    ]);

    $array[[[]]] = 'bar';
    $array[['foo', []]] = 'baz';
    $array[['foo', []]] = 'qux';

    var_dump($array->toArray());

    // => array(2) {
    //    ["foo"]=>
    //        array(2) {
    //            [0]=>
    //                string(3) "baz"
    //            [1]=>
    //                string(3) "qux"
    //        }
    //    [0]=>
    //        string(3) "bar"
    // }
```

### 4. offsetUnset:

[](#4-offsetunset)

You can unset nested key.

```
    $array = new CompositeKeyArray([
        'foo' => [
            'bar' => 'baz'
        ]
    ]);

    unset($array[['foo', 'bar']]);

    var_dump($array['foo']); // => array(0) {}
```

After nested manipulations you might want to get back the **real array**. This can be done by calling `$array->toArray()`.

XPath Key Array
---------------

[](#xpath-key-array)

**This is not a real xpath!** This class instead of array-like key users string of keys delimited with `/`.

```
    $array = new XPathKeyArray([
        'foo' => [
            'bar' => 'baz'
        ]
    ]);

    var_dump($array['foo/bar']); // => string(3) "baz"
```

This one was inspired by [an old article](http://codeaid.net/php/get-values-of-multi-dimensional-arrays-using-xpath-notation).

Compared to `CompositeKeyArray`, `XPathKeyArray` has some limitations:

1. You cannot use keys with `/` in them.
2. You cannot use `null` as key.

Dotted Key Array
----------------

[](#dotted-key-array)

This class instead of array-like key users string of keys delimited with `.`.

```
    $array = new DottedKeyArray([
        'foo' => [
            'bar' => 'baz'
        ]
    ]);

    var_dump($array['foo.bar']); // => string(3) "baz"
```

Compared to `CompositeKeyArray`, `DottedKeyArray` has some limitations:

1. You cannot use keys with `.` in them.
2. You cannot use `null` as key.

One-off Array
-------------

[](#one-off-array)

Sometimes you want to get value from an array by key and `unset` this key after that. The `OneOffArray` class helps you with this.

Again this class can be used in combination with `CompositeKeyArray` or its descendents: `XPathKeyArray` or `DottedKeyArray`. Actually, it can be used in combination with any object that implemets `ArrayAccess`.

Write-once Array
----------------

[](#write-once-array)

If you want to be sure that each offset in your array would be written only once you can use `WriteOnceArray`. If you try to set one particular offset more than one time `IllegalOffsetException` will be thrown:

```
$array = new WriteOnceArray();

$array['foo'] = 'bar'; // => OK
$array['foo'] = 'baz'; // => throws `IllegalOffsetException`
```

Because `offsetExists` method is used in order to ensure write-once behaviour, `offsetUnset` method call is illegal:

```
$array = new WriteOnceArray([
    'foo' => 'bar',
]);

unset($array['foo']); // => throws `IllegalOffsetUnsetMethodCallException`
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

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 ~154 days

Total

2

Last Release

2619d ago

Major Versions

v1.0 → v2.02019-03-08

PHP version history (2 changes)v1.0PHP &gt;=5.6

v2.0PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/4d447606cedb25aa0f2e4a57788d793bb46215aadaa5841b8ccf66f9477e5267?d=identicon)[sevavietl](/maintainers/sevavietl)

---

Top Contributors

[![Sevavietl](https://avatars.githubusercontent.com/u/1844827?v=4)](https://github.com/Sevavietl "Sevavietl (19 commits)")

---

Tags

arrayaccessarraysone-offphprecursionarrays

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/thorough-php-arrays/health.svg)

```
[![Health](https://phpackages.com/badges/thorough-php-arrays/health.svg)](https://phpackages.com/packages/thorough-php-arrays)
```

###  Alternatives

[ilya/belt

A handful of tools for PHP developers.

71020.8k1](/packages/ilya-belt)[minwork/array

Pack of advanced array functions specifically tailored for: associative (assoc) array, multidimensional array, array of objects and handling nested array elements

66256.1k5](/packages/minwork-array)[sarhan/php-flatten

Flattens multidimensional arrays, traversables and vars into one dimensional array.

21177.7k1](/packages/sarhan-php-flatten)[dotty/dotty

Easy access to array data using dot notation

1293.7k1](/packages/dotty-dotty)[rotexsoft/versatile-collections

A collection package that can be extended to implement things such as a Dependency Injection Container, RecordSet objects for housing database records, a bag of http cookies, or technically any collection of items that can be looped over and whose items can each be accessed using array-access syntax or object property syntax.

186.0k1](/packages/rotexsoft-versatile-collections)[glowy/arrays

Arrays Component provide a fluent, object-oriented interface for working with arrays, allowing you to chain multiple arrays operations together using a more readable syntax compared to traditional PHP arrays functions.

153.2k3](/packages/glowy-arrays)

PHPackages © 2026

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