PHPackages                             spatie/array-functions - 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. spatie/array-functions

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

spatie/array-functions
======================

Some handy array helpers

1.9.5(1y ago)24561.1k↑24.1%402MITPHPPHP ^7.4|^8.0CI passing

Since May 20Pushed 4mo ago13 watchersCompare

[ Source](https://github.com/spatie/array-functions)[ Packagist](https://packagist.org/packages/spatie/array-functions)[ Docs](https://github.com/spatie/array-functions)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/spatie-array-functions/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (16)Used By (2)

Some handy array functions
==========================

[](#some-handy-array-functions)

[![Latest Version on Packagist](https://camo.githubusercontent.com/77cbc36bebd08d7c9a029af19c520220ea050f299d42d1a3aaeab130f593fe11/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f61727261792d66756e6374696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/array-functions)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/a417705a45ac3af9f79927e5b50de6dd18c3d63729d6685e2570f83e8978324e/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f61727261792d66756e6374696f6e732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/array-functions)[![Quality Score](https://camo.githubusercontent.com/97796b0fb80854d22868c4df608b5acefe53034faede68a7e7e9d9806b8a8f1a/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f61727261792d66756e6374696f6e732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/array-functions)[![SensioLabsInsight](https://camo.githubusercontent.com/5d2e01ff8ab92fafa8582351146772ec2b02f5882333cd87bab2d7fb354c9726/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f38363033363464352d316437342d346366382d626462312d6335653138636463386137302f6d696e692e706e67)](https://insight.sensiolabs.com/projects/860364d5-1d74-4cf8-bdb1-c5e18cdc8a70)[![Total Downloads](https://camo.githubusercontent.com/1afdf75001486ebd43a95977588554a7c1db6abbb76af8904f47f5b7973eace0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f61727261792d66756e6374696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/array-functions)

This package provides some handy array functions.

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/26fba8e6768ad115f87402dc1672b952efb88561c56b6ec6b2cf60aa904bcfe0/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f61727261792d66756e6374696f6e732e6a70673f743d31)](https://spatie.be/github-ad-click/array-functions)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Postcardware
------------

[](#postcardware)

You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Install
-------

[](#install)

You can install this package via composer:

```
composer require spatie/array-functions
```

Usage
-----

[](#usage)

The following functions are provided in the `Spatie`-namespace:

### array\_rand\_value

[](#array_rand_value)

```
/**
 * Get a random value from an array.
 *
 * @param array $array
 * @param int $numReq  The amount of values to return
 * @return mixed
 */
function array_rand_value(array $array, $numReq = 1)
```

### array\_rand\_weighted

[](#array_rand_weighted)

```
/**
 * Get a random value from an array, with the ability to skew the results.
 * Example: array_rand_weighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar.
 *
 * @param array $key
 *
 * @return mixed
 */
function array_rand_weighted(array $array)
```

### values\_in\_array

[](#values_in_array)

```
/**
 * Determine if all given needles are present in the haystack.
 *
 * @param array|string $needles
 * @param array $haystack
 *
 * @return bool
 */
function values_in_array($needles, array $haystack)
```

### array\_keys\_exist

[](#array_keys_exist)

```
/**
 * Determine if all given needles are present in the haystack as array keys.
 *
 * @param array|string $needles
 * @param array $haystack
 *
 * @return bool
 */
function array_keys_exist($needles, array $haystack)
```

### array\_split\_filter

[](#array_split_filter)

```
/**
 * Returns an array with two elements.
 *
 * Iterates over each value in the array passing them to the callback function.
 * If the callback function returns true, the current value from array is returned in the first
 * element of result array. If not, it is return in the second element of result array.
 *
 * Array keys are preserved.
 *
 * @param array $array
 * @param callable $callback
 * @return array
 *
 */
function array_split_filter(array $array, callable $callback)
```

### array\_split

[](#array_split)

```
/**
 * Split an array in the given amount of pieces.
 *
 * @param array $array
 * @param int $numberOfPieces
 * @param bool $preserveKeys
 * @throws \InvalidArgumentException
 * @return array
 */
function array_split(array $array, $numberOfPieces = 2, $preserveKeys = false)
```

### array\_merge\_values

[](#array_merge_values)

```
/**
 * Returns an array with the unique values from all the given arrays
 *
 * @param \array[] $arrays
 * @return array
 */
function array_merge_values(array ... $arrays)
```

### array\_flatten

[](#array_flatten)

```
/**
 * Flatten an array of arrays. The `$levels` parameter specifies how deep you want to
 * recurse in the array. If `$levels` is -1, the function will recurse infinitely.
 *
 * @param array $array
 * @param int $levels
 *
 * @return array
 */
function array_flatten(array $array, $levels = -1)
```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

You can run the tests with:

```
vendor/bin/phpunit
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you've found a bug regarding security please mail  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

About Spatie
------------

[](#about-spatie)

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

License
-------

[](#license)

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

###  Health Score

57

—

FairBetter than 98% of packages

Maintenance60

Regular maintenance activity

Popularity49

Moderate usage in the ecosystem

Community29

Small or concentrated contributor base

Maturity76

Established project with proven stability

 Bus Factor1

Top contributor holds 60.2% 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 ~254 days

Recently: every ~489 days

Total

15

Last Release

458d ago

PHP version history (3 changes)1.0.0PHP &gt;=5.6.0

1.9.0PHP ^7.2

1.9.4PHP ^7.4|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (56 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (8 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (8 commits)")[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (5 commits)")[![bu4ak](https://avatars.githubusercontent.com/u/17143820?v=4)](https://github.com/bu4ak "bu4ak (3 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (3 commits)")[![BackEndTea](https://avatars.githubusercontent.com/u/14289961?v=4)](https://github.com/BackEndTea "BackEndTea (2 commits)")[![Chris8934](https://avatars.githubusercontent.com/u/44963939?v=4)](https://github.com/Chris8934 "Chris8934 (2 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![Littlesqx](https://avatars.githubusercontent.com/u/16516151?v=4)](https://github.com/Littlesqx "Littlesqx (1 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (1 commits)")[![Tjoosten](https://avatars.githubusercontent.com/u/5157609?v=4)](https://github.com/Tjoosten "Tjoosten (1 commits)")[![chapeupreto](https://avatars.githubusercontent.com/u/834048?v=4)](https://github.com/chapeupreto "chapeupreto (1 commits)")

---

Tags

arrayfunctionsphpspatiehelperarrayhandy

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/spatie-array-functions/health.svg)

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

###  Alternatives

[spatie/string

String handling evolved

5604.6M24](/packages/spatie-string)[bocharsky-bw/arrayzy

A native PHP arrays easy manipulation library in OOP way.

38425.4k](/packages/bocharsky-bw-arrayzy)[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)[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)[bayfrontmedia/php-array-helpers

Helper class to provide useful array functions.

1413.4k23](/packages/bayfrontmedia-php-array-helpers)

PHPackages © 2026

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