PHPackages                             rexlabs/utility-belt - 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. rexlabs/utility-belt

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

rexlabs/utility-belt
====================

A set of helpful utilities for working with data, collections (arrays of arrays) and arrays in php

4.0.0(2y ago)393.3k—9.4%2MITPHPPHP &gt;=7.4 &lt;8.3

Since Feb 25Pushed 2y ago3 watchersCompare

[ Source](https://github.com/rexlabsio/utility-belt-php)[ Packagist](https://packagist.org/packages/rexlabs/utility-belt)[ RSS](/packages/rexlabs-utility-belt/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (5)Used By (2)

Deprecated
==========

[](#deprecated)

This library isn't in active development. Various other collection libraries provide most functionality that we need out of the box.

Bug fixes only.

UtilityBelt
===========

[](#utilitybelt)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/135b93f6ef29b53a0fe0aeaf2d1423b16ef5aab1502843f1887cbc33a32b07b2/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7265786c6162732f7574696c6974792d62656c742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/rexlabs/utility-belt)[![Coverage Status](https://camo.githubusercontent.com/13cdfac37e2ee59fbb183ddf37b50e5bf90a104629c79eba0dd6301c9674912f/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f7265786c6162732f7574696c6974792d62656c742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/repos/rexlabs/utility-belt/badge.svg?branch=master)

Every time we start a new php project that requires even a small amount of data wrangling, we find ourselves re-writing the same helper functions over and over again.

This package provides some key functions that make it easier to perform common tasks like:

- finding and filtering values in collections of arrays
- grouping values in collections by properties
- extracting values from deeply nested associative arrays
- recursive array mapping
- determining if an array is associative
- and more...

These functions are all provided without pulling in any heavy framework dependencies or third party packages.

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

[](#installation)

Install the latest version with:

```
$ composer require rexlabs/utility-belt

```

CollectionUtility
-----------------

[](#collectionutility)

### filterWhere / filterWhereNot

[](#filterwhere--filterwherenot)

*(array $items, array $properties)*

Some extremely common functionality involves filtering collections (arrays of rows) based on a property list. The code required for this via array\_filter tends to become fairly bloated and makes the purpose of the underlying code harder to understand. These functions provide a simpler way to perform these kinds of filters.

All comparisons below can be run as case insensitive or strict checks.

```
$items = [
    [
        "name"=>"john",
        "age"=>18
    ],
    [
        "name"=>"mary",
        "age"=>19
    ],
    [
        "name"=>"william",
        "age"=>18,
        "dog"=>[
            "name"=>"betty"
        ]
    ]
];

CollectionUtility::filterWhere($items,["age"=>18])
// [["name"=>"john",...], ["name"=>"william",...]];

CollectionUtility::filterWhereNot($items,["age"=>18])
// [["name"=>"mary",...]]

CollectionUtility::filterWhere($items,["dog.name"=>"betty"])
// [["name"=>"william",...]]

CollectionUtility::filterWhere($items,[["dog.name"=>"betty"],["name"=>"john"]]);
// [["name"=>"william",...], ["name"=>"john"]]
```

### findWhere / findWhereNot

[](#findwhere--findwherenot)

*(array $items, array $properties)*

Similar to the filter methods but returns the first matching result and optionally the key where it was found.

### groupByProperty / keyByProperty

[](#groupbyproperty--keybyproperty)

*(array $array, $key)*

Re-organises rows in a collection under the values of one or more properties.

The difference between the two methods is that key by property allows for only a single row per property in the result while grouping will return an array of collections.

```
$items = [
    ["name"=>"john","dog"=>["name"=>"william"]],
    ["name"=>"frank","dog"=>["name"=>"william"]],
    ["name"=>"dodd","dog"=>["name"=>"bruce"]],
];

CollectionUtility::keyByProperty($items,"dog.name")
[
   "william"=>["name"=>"frank","dog"=>["name"=>"william"]],
   "bruce"=>["name"=>"dodd","dog"=>["name"=>"bruce"]],
]

CollectionUtility::keyByProperty($items,["name","dog.name"])
[
    "john.william"=>["name"=>"john","dog"=>["name"=>"william"]],
    "frank.william"=>["name"=>"frank","dog"=>["name"=>"william"]],
    "dodd.bruce"=>["name"=>"dodd","dog"=>["name"=>"bruce"]],
]

CollectionUtility::groupByProperty($items,"dog.name")
[
    "william"=>[
        ["name"=>"john","dog"=>["name"=>"william"]],
        ["name"=>"frank","dog"=>["name"=>"william"]],
    ],
    "bruce"=>[
        ["name"=>"dodd","dog"=>["name"=>"bruce"]],
    ]
]
```

### sort / asort

[](#sort--asort)

*(array $array, $property, $sort\_direction = self::SORT\_DIRECTION\_ASCENDING, $sort\_flags=SORT\_REGULAR)*

Sort a collection using a property or nested property

```
$items = [
    ["string"=>"a", "nested"=>["number"=>1]],
    ["string"=>"d", "nested"=>["number"=>3]],
    ["string"=>"c", "nested"=>["number"=>3]],
    ["string"=>"b", "nested"=>["number"=>2]],
];

CollectionUtility::sort($items, "nested.number", CollectionUtility::SORT_DIRECTION_DESCENDING, SORT_NUMERIC);
[
    ["string"=>"c", "nested"=>["number"=>3]],
    ["string"=>"d", "nested"=>["number"=>3]],
    ["string"=>"b", "nested"=>["number"=>2]],
    ["string"=>"a", "nested"=>["number"=>1]]
]

```

### keepKeys

[](#keepkeys)

*(array $array, array $keys, $removal\_action = CollectionUtility::REMOVAL\_ACTION\_DELETE)*

Remove (or nullify) all keys in each collection item except for those specified in the keys arg

```
$items = [
    ["animal"=>"dog","name"=>"John","weather"=>"mild"],
    ["animal"=>"cat","name"=>"William"]
];

CollectionUtility::keepKeys($items, ["animal","weather"], CollectionUtility::REMOVAL_ACTION_DELETE);
[
    ["animal"=>"dog", "weather"=>"mild"],
    ["animal"=>"cat"]
]

CollectionUtility::keepKeys($items, ["animal","weather"], CollectionUtility::REMOVAL_ACTION_NULLIFY);
[
    ["animal"=>"dog", "name"=>null, ""weather"=>"mild"],
    ["animal"=>"cat", "name"=>null]
]

```

### removeKeys

[](#removekeys)

*(array $array, array $keys, $removal\_action = CollectionUtility::REMOVAL\_ACTION\_DELETE)*

Remove (or nullify) any keys provided in the $keys argument in each collection item.

```
$items = [
    ["animal"=>"dog","name"=>"John","weather"=>"mild"],
    ["animal"=>"cat","name"=>"William"]
];

CollectionUtility::removeKeys($items, ["animal","weather"], CollectionUtility::REMOVAL_ACTION_DELETE);
[
    ["name"=>"John"],
    ["name"=>"William"]
]

```

### random

[](#random)

*(array $array)*

Get a random item from an array

### shuffle

[](#shuffle)

*(array $array)*

Shuffle an array and return the result

ArrayUtility
------------

[](#arrayutility)

### dotRead

[](#dotread)

*(array $array, $property, $default\_value=null)*

```
$array = [
    "a"=>[
        "very"=>[
            "deep"=>[
                "hole"=>"with a prize at the bottom"
            ]
        ]
    ]
]

ArrayUtility::dotRead($array, "a.very.shallow.hole", "no prize!")
"no prize!"

ArrayUtility::dotRead($array, "a.very.deep.hole")
"with a prize at the bottom"
```

### dotWrite

[](#dotwrite)

*(array $array, $property, $value)*

Original array:

```
$array = [
    "a"=>[
        "very"=>[
            "deep"=>[
                "hole"=>"with a prize at the bottom"
            ]
        ]
    ],
    "i"=>[
        "like"=>"candy"
    ]
]
```

Return a new array containing the updated property:

```
$array = ArrayUtility::dotWrite($array, "a.very.deep.hole", "no prize!");
$array - ArrayUtility::dotWrite($array, "i.like", ["carrots","broccoli"]);
```

Array is now:

```
$array = [
    "a"=>[
        "very"=>[
            "deep"=>[
                "hole"=>"no prize!"
            ]
        ]
    ],
    "i"=>[
        "like"=>["carrots","broccoli"]
    ]
]
```

Set new properties:

```
$array = ArrayUtility::dotWrite($array, "a.very.shallow.hole", "that may contain a prize!");
$array = ArrayUtility::dotWrite($array, "i.also.like", "blue skies");
```

Array is now:

```
$array = [
    "a"=>[
        "very"=>[
            "deep"=>[
                "hole"=>"no prize!"
            ]
        ]
    ],
    "a"=>[
        "very"=>[
            "shallow"=>[
                "hole"=>"that may contain a prize!"
            ]
        ]
    ],
    "i"=>[
        "like"=>["carrots","broccoli"]
        "also"=>[
            "like"=>"blue skies"
        ]
    ]
]
```

### dotMutate

[](#dotmutate)

*(array $array, $property, $value)*

This method is similar to `dotWrite()` except the array is passed by reference so that property changes directly mutate the given array.

Original array:

```
$array = [
    "a"=>[
        "very"=>[
            "deep"=>[
                "hole"=>"with a prize at the bottom"
            ]
        ]
    ],
    "i"=>[
        "like"=>"candy"
    ]
]
```

Mutate the array with the updated property:

```
ArrayUtility::dotMutate($array, "a.very.deep.hole", "no prize!");
ArrayUtility::dotMutate($array, "i.like", ["carrots","broccoli"]);
```

Array is now:

```
$array = [
    "a"=>[
        "very"=>[
            "deep"=>[
                "hole"=>"no prize!"
            ]
        ]
    ],
    "i"=>[
        "like"=>["carrots","broccoli"]
    ]
]
```

Set new properties:

```
ArrayUtility::dotMutate($array, "a.very.shallow.hole", "that may contain a prize!");
ArrayUtility::dotMutate($array, "i.also.like", "blue skies");
```

Array is now:

```
$array = [
    "a"=>[
        "very"=>[
            "deep"=>[
                "hole"=>"no prize!"
            ]
        ]
    ],
    "a"=>[
        "very"=>[
            "shallow"=>[
                "hole"=>"that may contain a prize!"
            ]
        ]
    ],
    "i"=>[
        "like"=>["carrots","broccoli"]
        "also"=>[
            "like"=>"blue skies"
        ]
    ]
]
```

### flatten / inflate

[](#flatten--inflate)

*(array $array)*

```
$items = [
    "a"=>[
        "name"=>"jimmy",
        "fruits"=>["apple","banana"]
    ],
    "b"=>[
        "name"=>"william",
        "age"=>18,
        "dog"=>[
            "name"=>"betty"
        ]
    ]
]

ArrayUtility::flatten($items);
[
    "a.name"=>"jimmy",
    "a.fruits"=>["apple","banana"], //by default these are not flattened
    "b.name"=>"william",
    "b.age"=>18,
    "b.dog.name"=>"betty"
]

ArrayUtility::inflate($flattenedArray);
[
    "a"=>[
        "name"=>"jimmy",
        "fruits"=>["apple","banana"]
    ],
    "b"=>[
        "name"=>"william",
        "age"=>18,
        "dog"=>[
            "name"=>"betty"
        ]
    ]
]

```

### isAssoc

[](#isassoc)

*(array $array)*

Quick test to determine whether an array is associative or not.

```
ArrayUtility::isAssoc(["a"=>1,"b"=>2])
true

ArrayUtility::isAssoc(["a","b"])
false

```

### keepKeys

[](#keepkeys-1)

*(array $array, array $keys, ArrayUtility::REMOVAL\_ACTION\_DELETE)*

Same as CollectionUtility::keepKeys but operates on a basic array instead of a collection of arrays.

### removeKeys

[](#removekeys-1)

*(array $array, array $keys, ArrayUtility::REMOVAL\_ACTION\_DELETE)*

Same as CollectionUtility::removeKeys but operates on a basic array instead of a collection of arrays.

### mapRecursive

[](#maprecursive)

*(array $array, callable $callback, $map\_arrays = false)*

Like array walk recursive but doesn't mutate the array. Also, callback receives three arguments: $value, $key, $array

### map

[](#map)

*(array $array, callable $callback)*

Like array\_map but the callback receives three arguments: $value, $key, $array

### first

[](#first)

*(array $array)*

Returns the first element from an array. More useful than reset() because it can handle the result of a function.

### last

[](#last)

*(array $array)*

Returns the last element from an array. More useful than end() because it can handle the result of a function.

### firstKey

[](#firstkey)

*(array $array)*

Returns the key of the first element in an array.

### lastKey

[](#lastkey)

*(array $array)*

Returns the key of the last element in an array.

Install
-------

[](#install)

Install `UtilityBelt` using Composer.

```
$ composer require rexlabs/utility-belt
```

Testing
-------

[](#testing)

`UtilityBelt` has a [PHPUnit](https://phpunit.de) test suite. To run the tests, run the following command from the project folder.

```
$ composer test
```

Credits
-------

[](#credits)

- [Alex Babkov](https://github.com/ababkov)
- [All Contributors](https://github.com/rexlabs/utility-belt/graphs/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 51.6% 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 ~985 days

Total

3

Last Release

1035d ago

Major Versions

3.0.1 → 4.0.02023-07-19

PHP version history (2 changes)3.0.0PHP ^7.0

4.0.0PHP &gt;=7.4 &lt;8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/18f5613c1ca6527bdee951ce1ff9dd4a626700ca01a0b787397ad9128a9dfaae?d=identicon)[rexlabsio](/maintainers/rexlabsio)

---

Top Contributors

[![ababkov](https://avatars.githubusercontent.com/u/478307?v=4)](https://github.com/ababkov "ababkov (32 commits)")[![jodiedunlop](https://avatars.githubusercontent.com/u/4055554?v=4)](https://github.com/jodiedunlop "jodiedunlop (20 commits)")[![trycatchjames](https://avatars.githubusercontent.com/u/2120124?v=4)](https://github.com/trycatchjames "trycatchjames (8 commits)")[![theharvester](https://avatars.githubusercontent.com/u/67521294?v=4)](https://github.com/theharvester "theharvester (2 commits)")

---

Tags

arraycollectionsdot-notationfilteringphputility-libraryarrayutilityfiltercollectionutilitiesdot readnested readdot syntaxmap recursive

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/rexlabs-utility-belt/health.svg)

```
[![Health](https://phpackages.com/badges/rexlabs-utility-belt/health.svg)](https://phpackages.com/packages/rexlabs-utility-belt)
```

###  Alternatives

[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)[jbzoo/utils

Collection of PHP functions, mini classes and snippets for everyday developer's routine life.

8321.5M36](/packages/jbzoo-utils)[aimeos/map

Easy and elegant handling of PHP arrays as array-like collection objects similar to jQuery and Laravel Collections

4.2k412.9k11](/packages/aimeos-map)[voku/arrayy

Array manipulation library for PHP, called Arrayy!

4875.5M16](/packages/voku-arrayy)[vaimo/composer-patches

Applies a patch from a local or remote file to any package that is part of a given composer project. Patches can be defined both on project and on package level. Optional support for patch versioning, sequencing, custom patch applier configuration and patch command for testing/troubleshooting added patches.

2994.3M16](/packages/vaimo-composer-patches)[athari/yalinqo

YaLinqo, a LINQ-to-objects library for PHP

4561.2M5](/packages/athari-yalinqo)

PHPackages © 2026

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