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

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

bkvfoundry/utility-belt
=======================

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

2.2.1(8y ago)543.4k↓26.6%4[1 issues](https://github.com/bkvfoundry/utility-belt/issues)MITPHPPHP ^5.5 || ^7.0

Since Dec 11Pushed 3y ago2 watchersCompare

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

READMEChangelog (4)Dependencies (2)Versions (7)Used By (0)

Deprecated
==========

[](#deprecated)

**UPDATE!: UtilityBelt has a new home at Rex Labs**

- [Github Repository](https://github.com/rexlabsio/utility-belt-php)
- [Composer Package](https://packagist.org/packages/rexlabs/utility-belt)

**Please update your projects to use the new package as it will no longer be maintained under the bkvfoundry repository**

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

[](#utilitybelt)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/ee1e40b441a02fd61c320053dc5f3252b620bbce91fc6f0c88e5a89c8f6b1d73/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f626b76666f756e6472792f7574696c6974792d62656c742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/bkvfoundry/utility-belt)[![Coverage Status](https://camo.githubusercontent.com/cf276c251a386a601b45cc1583d1ac5e68570e9cc8e3d339d0105057588d603b/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f626b76666f756e6472792f7574696c6974792d62656c742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/repos/bkvfoundry/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 bkvfoundry/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 bkvfoundry/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/bkvfoundry/utility-belt/graphs/contributors)

License
-------

[](#license)

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

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 63.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 ~159 days

Recently: every ~197 days

Total

6

Last Release

3013d ago

Major Versions

1.0.1 → 2.0.02016-03-03

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/478307?v=4)[Alex Babkov](/maintainers/ababkov)[@ababkov](https://github.com/ababkov)

---

Top Contributors

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

---

Tags

arrayutilityfiltercollectionutilitiesdot readnested readdot syntaxmap recursive

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/bkvfoundry-utility-belt/health.svg)](https://phpackages.com/packages/bkvfoundry-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)
