PHPackages                             bayfrontmedia/php-array-helpers - 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. bayfrontmedia/php-array-helpers

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

bayfrontmedia/php-array-helpers
===============================

Helper class to provide useful array functions.

v2.2.0(10mo ago)1413.4k—3.6%120MITPHPPHP ^8.0

Since Jul 27Pushed 10mo ago2 watchersCompare

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

READMEChangelog (10)DependenciesVersions (12)Used By (20)

PHP array helpers
-----------------

[](#php-array-helpers)

PHP helper class to provide useful array functions.

- [License](#license)
- [Author](#author)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)

License
-------

[](#license)

This project is open source and available under the [MIT License](LICENSE).

Author
------

[](#author)

[![Bayfront Media](https://camo.githubusercontent.com/0c0163913b2092e97dbf9684970adaf86f2f25871298d3d28b2dff4bf75b2915/68747470733a2f2f63646e312e6f6e62617966726f6e742e636f6d2f62666d2f6272616e642f62666d2d6c6f676f2e737667)](https://camo.githubusercontent.com/0c0163913b2092e97dbf9684970adaf86f2f25871298d3d28b2dff4bf75b2915/68747470733a2f2f63646e312e6f6e62617966726f6e742e636f6d2f62666d2f6272616e642f62666d2d6c6f676f2e737667)

- [Bayfront Media homepage](https://www.bayfrontmedia.com?utm_source=github&utm_medium=direct)
- [Bayfront Media GitHub](https://github.com/bayfrontmedia)

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

[](#requirements)

- PHP `^8.0` (Tested up to `8.4`)

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

[](#installation)

```
composer require bayfrontmedia/php-array-helpers

```

Usage
-----

[](#usage)

- [dot](#dot)
- [undot](#undot)
- [set](#set)
- [has](#has)
- [get](#get)
- [pluck](#pluck)
- [forget](#forget)
- [except](#except)
- [exceptValues](#exceptvalues)
- [only](#only)
- [missing](#missing)
- [isMissing](#ismissing)
- [multisort](#multisort)
- [numericMultisort](#numericmultisort)
- [renameKeys](#renamekeys)
- [order](#order)
- [getRandomItems](#getrandomitems)
- [query](#query)
- [getAnyValues](#getanyvalues)
- [hasAnyValues](#hasanyvalues)
- [hasAllValues](#hasallvalues)
- [ensureHas](#ensurehas)

---

### dot

[](#dot)

**Description:**

Converts a multidimensional array to a single depth "dot" notation array, optionally prepending a string to each array key.

The key values will never be an array, even if empty. Empty arrays will be dropped.

**Parameters:**

- `$array` (array): Original array
- `$prepend = ''` (string): String to prepend

**Returns:**

- (array)

**Example:**

```
$array = [
    'name' => [
        'first_name' => 'John',
        'last_name' => 'Doe'
    ],
    'hobbies' => [ // This empty array will be dropped

    ]
];

$dot = Arr::dot($array);

```

---

### undot

[](#undot)

**Description:**

Converts array in "dot" notation to a standard multidimensional array.

**Parameters:**

- `$array` (array): Array in "dot" notation

**Returns:**

- (array)

**Example:**

```
$array = [
    'name.first_name' => 'John',
    'name.last_name' => 'Doe'
];

$undot = Arr::undot($array);

```

---

### set

[](#set)

**Description:**

Set an array item to a given value using "dot" notation.

**Parameters:**

- `$array` (array): Original array
- `$key` (string): Key to set in "dot" notation
- `$value` (mixed): Value of key

**Returns:**

- (void)

**Example:**

```
$array = [
    'name' => [
        'first_name' => 'John',
        'last_name' => 'Doe'
    ],
];

Arr::set($array, 'name.middle_name', 'Middle');

```

---

### has

[](#has)

**Description:**

Checks if array key exists and not null using "dot" notation.

**Parameters:**

- `$array` (array): Original array
- `$key` (string): Key to check in "dot" notation

**Returns:**

- (bool)

**Example:**

```
$array = [
    'name' => [
        'first_name' => 'John',
        'last_name' => 'Doe'
    ],
];

if (Arr::has($array, 'name.first_name')) {
    // Do something
}

```

---

### get

[](#get)

**Description:**

Get an item from an array using "dot" notation, returning an optional default value if not found.

**Parameters:**

- `$array` (array): Original array
- `$key` (string): Key to return in "dot" notation
- `$default = NULL` (mixed): Default value to return

**Returns:**

- (mixed)

**Example:**

```
$array = [
    'name' => [
        'first_name' => 'John',
        'last_name' => 'Doe'
    ],
];

echo Arr::get($array, 'name.first_name');

```

---

### pluck

[](#pluck)

**Description:**

Returns an array of values for a given key from an array using "dot" notation.

**Parameters:**

- `$array` (array): Original array
- `$value` (string): Value to return in "dot" notation
- `$key = NULL` (string|null): Optionally how to key the returned array in "dot" notation

**Returns:**

- (array)

**Example:**

```
$array = [
    [
        'user_id' => 110,
        'username' => 'John',
        'active' => true
    ],
    [
        'user_id' => 111,
        'username' => 'Jane',
        'active' => true
    ]
];

$array = Arr::pluck($array, 'username', 'user_id');

```

---

### forget

[](#forget)

**Description:**

Remove a single key, or an array of keys from a given array using "dot" notation.

**Parameters:**

- `$array` (array): Original array
- `$keys` (string|array): Key(s) to forget in "dot" notation

**Returns:**

- (void)

**Example:**

```
$array = [
    'name' => [
        'first_name' => 'John',
        'last_name' => 'Doe'
    ],
];

Arr::forget($array, 'name.last_name');

```

---

### except

[](#except)

**Description:**

Returns the original array except given key(s).

**Parameters:**

- `$array` (array): Original array
- `$keys` (string|array): Key(s) to remove

**Returns:**

- (array)

**Example:**

```
$array = [
    'user_id' => 110,
    'username' => 'John',
    'active' => true
];

$array = Arr::except($array, 'active');

```

---

### exceptValues

[](#exceptvalues)

**Description:**

Returns the original array except given value(s).

**Parameters:**

- `$array` (array): Original array
- `$values` (string|array): Values(s) to remove

**Returns:**

- (array)

**Example:**

```
$array = [
    'John',
    'Jane',
    'Bob'
];

$array = Arr::exceptValues($array, 'John');

```

---

### only

[](#only)

**Description:**

Returns only desired key(s) from an array.

**Parameters:**

- `$array` (array): Original array
- `$keys` (string|array): Key(s) to return

**Returns:**

- (array)

**Example:**

```
$array = [
    'user_id' => 110,
    'username' => 'John',
    'active' => true
];

$array = Arr::only($array, 'username');

```

---

### missing

[](#missing)

**Description:**

Returns array of missing keys from the original array, or an empty array if none are missing.

**Parameters:**

- `$array` (array): Original array
- `$keys` (array): Key(s) to check

**Returns:**

- (array)

**Example:**

```
$array = [
    'user_id' => 110,
    'username' => 'John',
    'active' => true
];

$missing = Arr::missing($array, [
    'active',
    'last_login'
]);

```

---

### isMissing

[](#ismissing)

**Description:**

Checks if keys are missing from the original array.

**Parameters:**

- `$array` (array): Original array
- `$keys` (array): Key(s) to check

**Returns:**

- (bool)

**Example:**

```
$array = [
    'user_id' => 110,
    'username' => 'John',
    'active' => true
];

if (Arr::isMissing($array, [
    'active',
    'last_login'
])) {
    // Do something
}

```

---

### multisort

[](#multisort)

**Description:**

Sort a multidimensional array by a given key in ascending (optionally, descending) order.

**Parameters:**

- `$array` (array): Original array
- `$key` (string): Key name to sort by
- `$descending = false` (bool): Sort descending

**Returns:**

- (array)

**Example:**

```
$clients = [
    [
        'first_name' => 'John',
        'last_name' => 'Doe'
    ],
    [
        'first_name' => 'Jane',
        'last_name' => 'Doe'
    ]
];

$sorted = Arr::multisort($clients, 'first_name');

```

---

### numericMultisort

[](#numericmultisort)

**Description:**

Sort a numerically indexed array of multidimensional arrays by a given key in ascending (optionally, descending) order.

**Parameters:**

- `$array` (array): Numerically indexed array of multidimensional arrays
- `$key` (string): Key name to sort by in dot notation
- `$descending = false` (bool): Sort descending

**Returns:**

- (array)

**Example:**

```
$contacts = [
    [
        'id' => 1,
        'contact' => [
            'name' => 'James'
        ]
    ],
    [
        'id' => 2,
        'contact' => [
            'name' => 'Bob'
        ]
    ],
    [
        'id' => 3,
        'contact' => [
            'name' => 'Carl'
        ]
    ]
];

$sorted = Arr::numericMultisort($contacts, 'contact.name');

```

---

### renameKeys

[](#renamekeys)

**Description:**

Rename array keys while preserving their order.

**Parameters:**

- `$array` (array): Original array
- `$keys` (array): Key/value pairs to rename

**Returns:**

- (array)

**Example:**

```
$user = [
    'UserID' => 5,
    'UserEmail' => 'name@example.com',
    'UserGroup' => 'Administrator'
];

$renamed = Arr::renameKeys($user, [
    'UserID' => 'id',
    'UserEmail' => 'email',
    'UserGroup' => 'group'
]);

```

---

### order

[](#order)

**Description:**

Order an array based on an array of keys.

Keys from the `$order` array which do not exist in the original array will be ignored.

**Parameters:**

- `$array` (array): Original array
- `$order` (array): Array of keys in the order to be returned

**Returns:**

- (array)

**Example:**

```
$address = [
    'street' => '123 Main St.',
    'state' => 'IL',
    'zip' => '60007',
    'city' => 'Chicago'
];

$order = [
    'street',
    'city',
    'state',
    'zip',
    'country'
];

$address = Arr::order($address, $order);

```

The above example will return the following array:

```
Array
(
    [street] => 123 Main St.
    [city] => Chicago
    [state] => IL
    [zip] => 60007
)

```

---

### getRandomItems

[](#getrandomitems)

**Description:**

Get random items from array.

If `$count` is greater than or equal to the number of items on the array, the original array is returned in random order.

**Parameters:**

- `$array` (array): Original array
- `$count` (int): Number of random items to return

**Returns:**

- (array)

**Example:**

```
$array = [
    'John',
    'Jane',
    'Bob'
];

$array = Arr::getRandomItems($array, 1);

```

---

### query

[](#query)

**Description:**

Convert array into a query string.

**Parameters:**

- `$array` (array): Original array

**Returns:**

- (string)

**Example:**

```
$array = [
    'first_name' => 'Jane',
    'last_name' => 'Doe'
];

echo Arr::query($array);

```

---

### getAnyValues

[](#getanyvalues)

**Description:**

Return an array of values which exist in a given array.

**Parameters:**

- `$array` (array)
- `$values` (array)

**Returns:**

- (array)

**Example:**

```
$array = [
    'name' => [
        'John',
        'Dave'
    ],
];

$existing_values = Arr::getAnyValues($array['name'], [
    'John',
    'Jane'
]);

```

---

### hasAnyValues

[](#hasanyvalues)

**Description:**

Do any values exist in a given array.

**Parameters:**

- `$array` (array)
- `$values` (array)

**Returns:**

- (bool)

**Example:**

```
$array = [
    'name' => [
        'John',
        'Dave'
    ],
];

if (Arr::hasAnyValues($array['name'], [
    'John',
    'Jane'
])) {
     // Do something
}

```

---

### hasAllValues

[](#hasallvalues)

**Description:**

Do all values exist in a given array.

**Parameters:**

- `$array` (array)
- `$values` (array)

**Returns:**

- (bool)

**Example:**

```
$array = [
    'name' => [
        'John',
        'Dave'
    ],
];

if (Arr::hasAllValues($array['name'], [
    'John',
    'Jane'
])) {
     // Do something
}

```

---

### ensureHas

[](#ensurehas)

**Description:**

Ensure a numerically indexed array of arrays has a given item based on a unique key.

**Parameters:**

- `$array` (array)
- `$item` (array): Item to exist
- `$unique_key` (string): Unique array key

**Returns:**

- (bool)

**Example:**

```
$contacts = [
    [
        'id' => 1,
        'contact' => [
            'name' => 'James'
        ]
    ],
    [
        'id' => 2,
        'contact' => [
            'name' => 'Bob'
        ]
    ],
    [
        'id' => 3,
        'contact' => [
            'name' => 'Carl'
        ]
    ]
];

$item = [
    'id' => 2,
    'contact' => [
        'name' => 'Bob'
    ]
];

$contacts = Arr::ensureHas($contacts, $item, 'id');

```

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance55

Moderate activity, may be stable

Popularity34

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity66

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

Recently: every ~227 days

Total

11

Last Release

300d ago

Major Versions

v1.3.1 → v2.0.02023-01-26

PHP version history (2 changes)1.0.0PHP &gt;=7.1.0

v2.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/5ad62c8d0e69358fd63b16fdaa71d5359231cd0cf660bbc3419071dc705c63a8?d=identicon)[bayfrontmedia](/maintainers/bayfrontmedia)

---

Top Contributors

[![robinsonjohn](https://avatars.githubusercontent.com/u/24327848?v=4)](https://github.com/robinsonjohn "robinsonjohn (21 commits)")

---

Tags

arrayfunctionhelperhelpersphpphphelperarrayhelpersfunction

### Embed Badge

![Health badge](/badges/bayfrontmedia-php-array-helpers/health.svg)

```
[![Health](https://phpackages.com/badges/bayfrontmedia-php-array-helpers/health.svg)](https://phpackages.com/packages/bayfrontmedia-php-array-helpers)
```

###  Alternatives

[laravelista/ekko

Framework agnostic PHP package for marking navigation items active.

278673.4k4](/packages/laravelista-ekko)[clausnz/php-helpers

A Collection of useful php helper functions.

388.7k](/packages/clausnz-php-helpers)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2516.7k](/packages/iteks-laravel-enum)

PHPackages © 2026

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