PHPackages                             theodorejb/array-utils - 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. theodorejb/array-utils

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

theodorejb/array-utils
======================

Useful functions for working with arrays

v2.1.1(1y ago)46.1k↓50%2MITPHPPHP &gt;=8.1CI passing

Since Jan 13Pushed 3mo ago2 watchersCompare

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

READMEChangelog (1)Dependencies (2)Versions (11)Used By (0)

ArrayUtils
==========

[](#arrayutils)

[![Packagist Version](https://camo.githubusercontent.com/7b48e7b1f57e0a09bd76b1177bcd8f66515381f9809725158f068acfe80a05d4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7468656f646f72656a622f61727261792d7574696c732e737667)](https://packagist.org/packages/theodorejb/array-utils)

ArrayUtils is a collection of useful PHP array functions.

Install via Composer
--------------------

[](#install-via-composer)

`composer require theodorejb/array-utils`

Methods
-------

[](#methods)

### containsAll

[](#containsall)

Returns true if all the needles are in the haystack.

```
use theodorejb\ArrayUtils\ArrayUtils;

$haystack = [1, 2, 3, 5, 8, 13];
$needles = [2, 13, 5];
ArrayUtils::containsAll($needles, $haystack); // true
ArrayUtils::containsAll($haystack, $needles); // false
```

### containsSame

[](#containssame)

Returns true if both arrays contain the same values (regardless of order).

```
use theodorejb\ArrayUtils\ArrayUtils;

$set1 = [1, 3, 5, 7];
$set2 = [3, 7, 5, 1];

ArrayUtils::containsSame($set1, $set2); // true
```

### groupRows

[](#grouprows)

Splits the iterable of arrays into groups when the value of one or more keys changes. The iterable must be sorted by the array keys used to group results.

```
use theodorejb\ArrayUtils\ArrayUtils;

$peoplePets = [
    ['lName' => 'Jordan', 'fName' => 'Jack', 'pet' => 'Scruffy'],
    ['lName' => 'Jordan', 'fName' => 'Jack', 'pet' => 'Spot'],
    ['lName' => 'Jordan', 'fName' => 'Jill', 'pet' => 'Paws'],
    ['lName' => 'Greene', 'fName' => 'Amy',  'pet' => 'Blackie'],
    ['lName' => 'Greene', 'fName' => 'Amy',  'pet' => 'Whiskers'],
    ['lName' => 'Greene', 'fName' => 'Amy',  'pet' => 'Paws'],
    ['lName' => 'Smith',  'fName' => 'Amy',  'pet' => 'Tiger'],
];

$grouped = [];

foreach (ArrayUtils::groupRows($peoplePets, 'fName') as $group) {
    $grouped[] = $group;
}

$expected = [
    [$peoplePets[0], $peoplePets[1]],
    [$peoplePets[2]],
    [$peoplePets[3], $peoplePets[4], $peoplePets[5], $peoplePets[6]],
];

var_dump($grouped === $expected); // bool(true)

////// Additional arguments can be passed to `groupRows` to group by more than one column:

$grouped = [];

foreach (ArrayUtils::groupRows($peoplePets, 'lName', 'fName') as $group) {
    $grouped[] = $group;
}

$expected = [
    [$peoplePets[0], $peoplePets[1]],
    [$peoplePets[2]],
    [$peoplePets[3], $peoplePets[4], $peoplePets[5]],
    [$peoplePets[6]],
];

var_dump($grouped === $expected); // bool(true)
```

### requireStrKey

[](#requirestrkey)

Returns the specified array key value if it is a string. Throws an exception otherwise.

```
use theodorejb\ArrayUtils\ArrayUtils;

$data = ['k' => 'val', 'i' => 1];
ArrayUtils::requireStrKey($data, 'k'); // val
ArrayUtils::requireStrKey($data, 'x'); // throws OutOfBoundsException
ArrayUtils::requireStrKey($data, 'i'); // throws UnexpectedValueException
```

### getOptionalStrKey

[](#getoptionalstrkey)

Returns the specified array key value if it is a string, or `null` if the array key doesn't exist. Throws an exception if the key exists but the value is not a string.

```
use theodorejb\ArrayUtils\ArrayUtils;

$data = ['k' => 'val', 'i' => 1];
ArrayUtils::getOptionalStrKey($data, 'k'); // val
ArrayUtils::getOptionalStrKey($data, 'x'); // null
ArrayUtils::getOptionalStrKey($data, 'i'); // throws UnexpectedValueException
```

### requireNumericKey

[](#requirenumerickey)

Returns the specified array key value as a float if it is an integer or float. Throws an exception otherwise.

```
use theodorejb\ArrayUtils\ArrayUtils;

$data = ['i' => 1, 'f' => 0.5, 'k' => 'val'];
ArrayUtils::requireNumericKey($data, 'i'); // 1.0
ArrayUtils::requireNumericKey($data, 'f'); // 0.5
ArrayUtils::requireNumericKey($data, 'x'); // throws OutOfBoundsException
ArrayUtils::requireNumericKey($data, 'k'); // throws UnexpectedValueException
```

### getOptionalNumericKey

[](#getoptionalnumerickey)

Returns the specified array key value as a float if it is an integer or float, or `null` if the array key doesn't exist. Throws an exception if the key exists but the value is not an integer or float.

```
use theodorejb\ArrayUtils\ArrayUtils;

$data = ['i' => 2, 'f' => 0.5, 'k' => 'val'];
ArrayUtils::getOptionalNumericKey($data, 'i'); // 2.0
ArrayUtils::getOptionalNumericKey($data, 'f'); // 0.5
ArrayUtils::getOptionalNumericKey($data, 'x'); // null
ArrayUtils::getOptionalNumericKey($data, 'k'); // throws UnexpectedValueException
```

### requireIntKey

[](#requireintkey)

Returns the specified array key value if it is an integer. Throws an exception otherwise.

```
use theodorejb\ArrayUtils\ArrayUtils;

$data = ['k' => 'val', 'i' => 1];
ArrayUtils::requireIntKey($data, 'i'); // 1
ArrayUtils::requireIntKey($data, 'x'); // throws OutOfBoundsException
ArrayUtils::requireIntKey($data, 'k'); // throws UnexpectedValueException
```

### getOptionalIntKey

[](#getoptionalintkey)

Returns the specified array key value if it is an integer, or `null` if the array key doesn't exist. Throws an exception if the key exists but the value is not an integer.

```
use theodorejb\ArrayUtils\ArrayUtils;

$data = ['k' => 'val', 'i' => 2];
ArrayUtils::getOptionalIntKey($data, 'i'); // 2
ArrayUtils::getOptionalIntKey($data, 'x'); // null
ArrayUtils::getOptionalIntKey($data, 'k'); // throws UnexpectedValueException
```

### requireBoolKey

[](#requireboolkey)

Returns the specified array key value if it is a boolean. Throws an exception otherwise.

```
use theodorejb\ArrayUtils\ArrayUtils;

$data = ['k' => 'val', 'b' => true];
ArrayUtils::requireBoolKey($data, 'b'); // true
ArrayUtils::requireBoolKey($data, 'x'); // throws OutOfBoundsException
ArrayUtils::requireBoolKey($data, 'k'); // throws UnexpectedValueException
```

### getOptionalBoolKey

[](#getoptionalboolkey)

Returns the specified array key value if it is a boolean, or `null` if the array key doesn't exist. Throws an exception if the key exists but the value is not a boolean.

```
use theodorejb\ArrayUtils\ArrayUtils;

$data = ['k' => 'val', 'b' => false];
ArrayUtils::getOptionalBoolKey($data, 'b'); // false
ArrayUtils::getOptionalBoolKey($data, 'x'); // null
ArrayUtils::getOptionalBoolKey($data, 'k'); // throws UnexpectedValueException
```

Author
------

[](#author)

Theodore Brown

License
-------

[](#license)

MIT

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance66

Regular maintenance activity

Popularity28

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity78

Established project with proven stability

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

Recently: every ~471 days

Total

8

Last Release

367d ago

Major Versions

v1.1.2 → v2.0.02023-02-08

PHP version history (5 changes)v1.0.0PHP &gt;=5.6

v1.1.1PHP &gt;=7.2

v1.1.2PHP &gt;=7.3

v2.0.0PHP &gt;=7.4

v2.1.0PHP &gt;=8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3053271?v=4)[Theodore Brown](/maintainers/theodorejb)[@theodorejb](https://github.com/theodorejb)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/theodorejb-array-utils/health.svg)

```
[![Health](https://phpackages.com/badges/theodorejb-array-utils/health.svg)](https://phpackages.com/packages/theodorejb-array-utils)
```

PHPackages © 2026

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