PHPackages                             mpetrovich/dash - 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. mpetrovich/dash

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

mpetrovich/dash
===============

A functional programming library for PHP. Inspired by Underscore, Lodash, and Ramda.

v5.2.0(2mo ago)10330.0k↓13.9%101MITPHPPHP &gt;=8.2CI passing

Since Sep 18Pushed 2mo ago9 watchersCompare

[ Source](https://github.com/mpetrovich/dash)[ Packagist](https://packagist.org/packages/mpetrovich/dash)[ Docs](https://github.com/mpetrovich/dash)[ RSS](/packages/mpetrovich-dash/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (10)Dependencies (6)Versions (43)Used By (1)

Dash [![Latest Stable Version](https://camo.githubusercontent.com/17be70bab3575f79392c5fb4ebaca25e5ca7aeb038e32a5be854a120fc6f0218/68747470733a2f2f706f7365722e707567782e6f72672f6d706574726f766963682f646173682f76657273696f6e)](https://packagist.org/packages/mpetrovich/dash) [![Build Status](https://camo.githubusercontent.com/7a57303e7bd6c195ff5d3dae37d5f470f3c393fa1eecb927c291631805858b64/68747470733a2f2f7472617669732d63692e6f72672f6d706574726f766963682f646173682e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mpetrovich/dash) [![codecov](https://camo.githubusercontent.com/c94a240f53eba2a7242a941b8088fcb5ba3674bf94bda835b24d8dfcf5acaf3c/68747470733a2f2f636f6465636f762e696f2f67682f6d706574726f766963682f646173682f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/mpetrovich/dash)
==============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#dash----)

**A functional programming library for PHP.** Inspired by Underscore, Lodash, and Ramda.

```
$avgMaleAge = Dash\chain([
	['name' => 'John', 'age' => 12, 'gender' => 'male'],
	['name' => 'Jane', 'age' => 34, 'gender' => 'female'],
	['name' => 'Pete', 'age' => 23, 'gender' => 'male'],
	['name' => 'Mark', 'age' => 11, 'gender' => 'male'],
	['name' => 'Mary', 'age' => 42, 'gender' => 'female'],
])
->filter(['gender', 'male'])
->map('age')
->average()
->value();

echo "Average male age is $avgMaleAge.";
```

#### Jump to:

[](#jump-to)

- [Functions / Operations](#operations)
- [Highlights](#highlights)
- [Why use Dash?](#why-use-dash)
- [Installation](#installation)
- [Usage](#usage)
    - [Standalone](#standalone)
    - [Chaining](#chaining)
    - [Supported data types](#supported-data-types)
    - [Currying](#currying)
    - [Lazy evaluation](#lazy-evaluation)
    - [Custom operations](#custom-operations)
    - [Tips](#tips)
- [Changelog](https://github.com/mpetrovich/dash/releases)
- [Contributing](CONTRIBUTING.md)

Operations
----------

[](#operations)

[View full list of operations here](docs/Operations.md)

Highlights
----------

[](#highlights)

- [Many data types supported](#supported-data-types): arrays, objects, generators, [`Traversable`](http://php.net/manual/en/class.traversable.php), [`DirectoryIterator`](http://php.net/manual/en/class.directoryiterator.php), and more
- [Chaining](#chaining)
- [Currying](#currying)
- [Lazy evaluation](#lazy-evaluation)
- [Custom operations](#custom-operations)
- Well-tested: Comprehensive tests with nearly 3,000 test cases and [100% code coverage](https://codecov.io/gh/mpetrovich/dash)

Why use Dash?
-------------

[](#why-use-dash)

PHP's built-in `array_*` functions are limited, difficult to compose, inconsistent, and don't work across many data types.

For instance, let's say we want to find the average age of males in this list:

```
$people = [
	['name' => 'John', 'age' => 12, 'gender' => 'male'],
	['name' => 'Jane', 'age' => 34, 'gender' => 'female'],
	['name' => 'Pete', 'age' => 23, 'gender' => 'male'],
	['name' => 'Mark', 'age' => 11, 'gender' => 'male'],
	['name' => 'Mary', 'age' => 42, 'gender' => 'female'],
];
```

Using PHP's built-in in functions, we might write something like this:

```
$males = array_filter($people, function ($person) {
	return $person['gender'] === 'male';
});
$avgMaleAge = array_sum(array_column($males, 'age')) / count($males);
```

Dash makes common data transformation operations simpler:

```
$avgMaleAge = Dash\chain($people)
	->filter(['gender', 'male'])
	->map('age')
	->average()
	->value();
```

This is just a tiny subset of what Dash can do. [**See the full list of operations here.**](docs/Operations.md)

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

[](#installation)

Requires PHP 8.2+ (tested on 8.2-8.5)

```
composer require mpetrovich/dash
```

Usage
-----

[](#usage)

Dash operations are pure functions that can be used alone or chained together.

### Standalone

[](#standalone)

Operations can be called as namespaced functions:

```
Dash\map([1, 2, 3], function ($n) { return $n * 2; });  // === [2, 4, 6]
```

or as static methods:

```
use Dash\Dash;

Dash::map([1, 2, 3], function ($n) { return $n * 2; });  // === [2, 4, 6]
```

`Dash\__` can also be used as an alias for `Dash\Dash`:

```
use Dash\__;

__::map([1, 2, 3], function ($n) { return $n * 2; });  // === [2, 4, 6]
```

### Chaining

[](#chaining)

Multiple operations can be chained in sequence using `chain()`. Call `value()` to return the final value.

```
$result = Dash\chain([1, 2, 3, 4, 5])
	->filter('Dash\isOdd')
	->map(function ($n) { return $n * 2; })
	->value();

// $result === [2, 6, 10]
```

To explicitly convert the value to an array or `stdClass`, use `arrayValue()` or `objectValue()` respectively:

```
$result = Dash\chain(['a' => 1, 'b' => 2, 'c' => 3])
	->filter('Dash\isOdd')
	->mapValues(function ($n) { return $n * 2; })
	->objectValue();

// $result === (object) ['a' => 2, 'c' => 6]
```

For convenience, `Dash\chain()` can be aliased to a global function using `addGlobalAlias()`. It only needs to be called once during your application bootstrap:

```
// In your application bootstrap:
Dash::addGlobalAlias('_Dash');

// Elsewhere:
$result = _Dash([1, 2, 3, 4, 5])
	->filter('Dash\isOdd')
	->map(function ($n) { return $n * 2; })
	->value();
```

Sometimes you don't need the return value of the chain. However, the chain isn't processed until `value()` is called. For semantic convenience, `run()` is also an alias for `value()`:

```
$chain = Dash\chain(range(1, 5))
	->reverse()
	->each(function ($n) {
		echo "T-minus $n...\n";
		sleep(1);
	});

// Nothing echoed yet

$chain->value();
// or
$chain->run();

// Echoes each of the following lines 1 second apart:
// T-minus 5...
// T-minus 4...
// T-minus 3...
// T-minus 2...
// T-minus 1...
```

### Supported data types

[](#supported-data-types)

Dash can work with a wide variety of data types, including:

- arrays
- objects (eg. `stdClass`)
- generators
- anything that implements the [`Traversable`](http://php.net/manual/en/class.traversable.php) interface
- [`DirectoryIterator`](http://php.net/manual/en/class.directoryiterator.php), which is also a `Traversable` but cannot normally be used with `iterator_to_array()` [due to a PHP bug](https://bugs.php.net/bug.php?id=49755). Dash works around this transparently.

#### Examples

[](#examples)

With an array:

```
Dash\chain([1, 2, 3, 4])
	->filter('Dash\isEven')
	->map(function ($value) {
		return $value * 2;
	})
	->value();
// === [4, 8]
```

With an object:

```
Dash\chain((object) ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4])
	->filter('Dash\isOdd')
	->keys()
	->join(', ')
	->value();
// === 'a, c'
```

With a `Traversable`:

```
Dash\chain(new ArrayObject(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]))
	->pick(['b', 'c'])
	->values()
	->sum()
	->value();
// === 5
```

With a generator:

```
$integers = function () {
	for ($int = 1; true; $int++) {
		yield $int;
	}
};

Dash\chain($integers())
	->filter('Dash\isOdd')
	->take(3)
	->reverse()
	->value();
// === [5, 3, 1]
```

Generator inputs are supported. Streamable operations are lazy by default when given generator input (for example, `filter()`, `map()`, `take()`, `dropWhile()`, and `takeWhile()`), while operations that inherently require full materialization accept generators and return eager arrays (for example, `sort()`, `reverse()`, `rotate()`, and `takeRight()`).

With a `DirectoryIterator`:

```
$iterator = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);

$filenames = Dash\chain($iterator)
	->reject(function ($fileinfo) {
		return $fileinfo->isDir();
	})
	->map(function ($fileinfo) {
		return pathinfo($fileinfo)['filename'];
	})
	->value();
```

### Currying

[](#currying)

[`curry()`](docs/Operations.md#curry) and related operations can be used to create curried functions from any callable:

```
function listThree($a, $b, $c) {
	return "$a, $b, and $c";
}

$listThree = Dash\curry('listThree');
$listTwo = $listThree('first');
$listTwo('second', 'third');  // === 'first, second, and third'
```

Most Dash functions have a curried version that accepts input data as the last parameter instead of as the first. Curried versions are located in the `Dash\Curry` namespace:

```
Dash\chain([
	'a' => 3,
	'b' => '3',
	'c' => 3,
	'd' => 3.0
])
->filter(Dash\Curry\identical(3))
->value();
// === ['a' => 3, 'c' => 3]
```

Similarly, [`partial()`](docs/Operations.md#partial) and related operations can be used to create partially-applied functions:

```
$greet = function ($greeting, $name) {
	return "$greeting, $name!";
};

$sayHello = Dash\partial($greet, 'Hello');
$sayHowdy = Dash\partial($greet, 'Howdy');

$sayHello('Mark');  // === 'Hello, Mark!'
$sayHowdy('Jane');  // === 'Howdy, Jane!'
```

### Lazy evaluation

[](#lazy-evaluation)

Chained operations are not evaluated until `value()` or `run()` is called. Furthermore, the input data can be changed and evaluated multiple times using `with()`. This makes it simple to create reusable chains:

```
$chain = Dash\chain()
	->filter('Dash\isOdd')
	->map(function ($n) { return $n * 2; });

$chain->with([1, 2, 3])->value();  // === [2, 6]
$chain->with([4, 5, 6, 7])->value();  // === [10, 14]
```

Chains can also be cloned and extended:

```
// …continued from above
$clone = clone $chain;
$clone->map(function ($n) { $n + 1; })
$clone->value();  // === [11, 15]

// The original chain is untouched
$chain->value();  // === [10, 14]
```

When `value()` is called, the result is cached until the chain is modified or the input is changed using `with()`.

### Custom operations

[](#custom-operations)

Custom operations can be added, retrieved, and removed using `setCustom()`, `getCustom()`, and `unsetCustom()`, respectively. `Dash\custom()` is also an alias for `Dash::getCustom()`:

```
Dash::setCustom('triple', function ($n) { return $n * 3; });

// Standalone
Dash::triple(4);  // === 12

// Chained
Dash\chain([1, 2, 3])
	->sum()
	->triple()
	->value();  // === 18

// As an argument
Dash\chain([1, 2, 3])
	->map('Dash\Dash::triple')
	->value();  // === [3, 6, 9]

// As an argument using the Dash::getCustom() method
Dash\chain([1, 2, 3])
	->map(Dash::getCustom('triple'))
	->value();  // === [3, 6, 9]

// Using the Dash\custom() operation
Dash\chain([1, 2, 3])
	->map(Dash\custom('triple'))
	->value();  // === [3, 6, 9]

Dash::unsetCustom('triple');
```

When chained, the current input is passed as the first parameter to the custom operation:

```
Dash::setCustom('divide', function($numerator, $denominator) { return $numerator / $denominator; });

Dash\chain(6)->divide(2)->value();  // === 2
```

### Tips

[](#tips)

If you find that Dash doesn't have an operation that you need, fear not. Custom logic can be added without giving up Dash chaining or other features. The simplest way to integrate missing operations is via the [`Dash\thru()`](docs/Operations.md#thru) operation, which allows custom logic to modify and seamlessly pass through its results to the next step in the chain.

For example, suppose we want to use `array_change_key_case()` and keep the usual Dash chaining semantics. With `thru()`, it's simple:

```
$result = Dash\chain(['one' => 1, 'two' => 2, 'three' => 3])
	->filter('Dash\isOdd')
	->thru(function($input) {
		return array_change_key_case($input, CASE_UPPER);
	})
	->keys()
	->value();

// $result === ['ONE', 'THREE']
```

Alternatively, if you find yourself needing to use `array_change_key_case()` often, it may be better to add a new custom operation:

```
Dash::setCustom('keyCase', function ($input, $case) {
	return array_change_key_case($input, $case);
});
```

which you can then use like any other chainable Dash method:

```
$result = Dash\chain(['one' => 1, 'two' => 2, 'three' => 3])
	->filter('Dash\isOdd')
	->keyCase(CASE_UPPER)
	->keys()
	->value();

// $result === ['ONE', 'THREE']
```

### Feedback

[](#feedback)

Found a bug or have a suggestion? Please [create a new GitHub issue](https://github.com/mpetrovich/dash/issues/new). We want your feedback!

###  Health Score

64

—

FairBetter than 99% of packages

Maintenance87

Actively maintained with recent releases

Popularity42

Moderate usage in the ecosystem

Community22

Small or concentrated contributor base

Maturity88

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 98.8% 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 ~110 days

Recently: every ~424 days

Total

36

Last Release

66d ago

Major Versions

v1.0.1 → v2.0.0-alpha2018-08-19

v1.1.0 → v2.0.02019-03-15

v2.1.0 → v3.0.02020-02-23

v3.1.0 → v4.0.02021-06-09

v4.0.2 → v5.0.02026-04-28

PHP version history (6 changes)v0.1.0PHP &gt;=5.3.0

v0.2.0PHP &gt;=5.4.0

v2.0.0-alphaPHP &gt;=5.5.0

v3.0.0PHP &gt;=5.6.0

v4.0.0PHP &gt;=7.4

v5.0.0PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/10fa9bcf8e66fc1a2c691627a7cef88dd2773964deb568744823372e4d567586?d=identicon)[mpetrovich](/maintainers/mpetrovich)

---

Top Contributors

[![mpetrovich](https://avatars.githubusercontent.com/u/1235062?v=4)](https://github.com/mpetrovich "mpetrovich (658 commits)")[![brandonramirez](https://avatars.githubusercontent.com/u/439728?v=4)](https://github.com/brandonramirez "brandonramirez (3 commits)")[![adam1658](https://avatars.githubusercontent.com/u/3508429?v=4)](https://github.com/adam1658 "adam1658 (2 commits)")[![dantswain](https://avatars.githubusercontent.com/u/463747?v=4)](https://github.com/dantswain "dantswain (1 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![rodrigoprimo](https://avatars.githubusercontent.com/u/77215?v=4)](https://github.com/rodrigoprimo "rodrigoprimo (1 commits)")

---

Tags

functional-programminglodashphpramdaunderscorephpfunctionalunderscorefunctional-programminglodashbottomlineunderbarramda

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/mpetrovich-dash/health.svg)

```
[![Health](https://phpackages.com/badges/mpetrovich-dash/health.svg)](https://phpackages.com/packages/mpetrovich-dash)
```

###  Alternatives

[maciejczyzewski/bottomline

A full-on PHP manipulation utility belt that provides support for working with arrays, objects, and iterables; a lodash or underscore equivalent for PHP.

475643.1k10](/packages/maciejczyzewski-bottomline)[lambdish/phunctional

λ PHP functional library

3642.1M24](/packages/lambdish-phunctional)[bdelespierre/underscore

Underscore.js port in PHP

6944.2k1](/packages/bdelespierre-underscore)[chemem/bingo-functional

A simple functional programming library.

707.0k3](/packages/chemem-bingo-functional)[krak/fn

Functional library for php with proper currying

2419.0k3](/packages/krak-fn)[transprime-research/piper

PHP Pipe method execution with values from chained method executions

174.7k2](/packages/transprime-research-piper)

PHPackages © 2026

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