PHPackages                             bdelespierre/underscore - 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. bdelespierre/underscore

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

bdelespierre/underscore
=======================

Underscore.js port in PHP

v0.2.0(10y ago)6944.2k↑635.7%16[4 issues](https://github.com/bdelespierre/php-underscore/issues)1LGPLPHPPHP &gt;=5.4.0

Since Jan 12Pushed 6y ago6 watchersCompare

[ Source](https://github.com/bdelespierre/php-underscore)[ Packagist](https://packagist.org/packages/bdelespierre/underscore)[ Docs](https://github.com/bdelespierre/underscore.php)[ RSS](/packages/bdelespierre-underscore/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (1)Versions (5)Used By (1)

[![Build Status](https://camo.githubusercontent.com/e701cf529aee605a1d4a0ed5f4f1d7aecdaef785b578939887f72518c04c0123/68747470733a2f2f7472617669732d63692e6f72672f6264656c65737069657272652f756e64657273636f72652e7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/bdelespierre/underscore.php)

» [Table of contents](#table-of-contents)

Underscore.php
==============

[](#underscorephp)

PHP lacks consistency, that's a fact. Many functions within a similar field — for instance array functions — may have inconsistent names and prototypes. Underscore.php aims to correct that by providing simple, consistent and data-type tolerant 80-odd functions that support both the usual functional suspects: map, select, invoke — as well as more specialized helpers: function binding, php templating, deep equality testing, and so on.

Underscore.php is **strongly** inspired by [Underscore.js](http://underscorejs.org/) and try to be consistent with it as much as possible (PHP language limitation doesn't allow full coverage — especialy for Functions functions...) Don't hesitate to [report](https://github.com/bdelespierre/underscore.php/issues) any discrepancy with Underscore.js.

Features
--------

[](#features)

- Made with ♥ for PHP 5.4+
- Type tolerant
- Triggers exceptions instead of errors
- Consistent function names / arguments
- Hassle-free chaining
- ERB-style templating
- Extensible

Heads up!
---------

[](#heads-up)

This library is in **beta** phase: you are strongly encouraged to try it and to contribute.Feel free to [file an issue](https://github.com/bdelespierre/underscore.php/issues) if you encounter a bug or an unexpected result.

About data-type tolerance
-------------------------

[](#about-data-type-tolerance)

Juggling with types in PHP can be tedious. Not only types are sometimes ambiguous, they don't fit in every API function/method. For instance, if you want to map every item from an iterator using [array\_map](http://php.net/manual/en/function.array-map.php), you have to translate it into an array first or write the mapping yourself using a loop. Same goes for [sort](http://php.net/manual/en/function.sort.php), [diff](http://php.net/manual/en/function.array-diff.php) or [filter](http://php.net/manual/en/function.array-filter.php)...

PHP is loosely typed, which means that the data you're manipulating are more important than their structure. Underscore.php understands that by providing a comprehensive interface that works with almost every data-type so you don't have to worry about whether you can or cannot use a function/method.

Basically, Underscore.php uses 3 main data-types:

- Scalar (integer, float, boolean, or string)
- Traversable (array, object, or iterator)
- Callable (closure, function, method, or runtime-created function)

When a fuction requires a Traversable as argument, you can provide either an array, an instance of stdClass — the default `(object)` casting — an Iterator or anything that implements the Traversable interface, such as a PDOStatement object. With certain functions like #extend, you can even extend an array with an object instance and everything will be fine.

```

```

[Table of contents](#table-of-contents) » [Collection Functions](#collection-functions)

### eachReference

[](#eachreference)

---

***Alias***: walk

***Description***: Does the very same job as each but provide a reference of every list item to the iterator function.

##### *Parameters*

[](#parameters-1)

- *list*: traversable, the list to iterate over
- *iterator*: callable, the iteration function
- *context*: object, optional, if provided will become the context of $iterator

##### *Prototype*

[](#prototype-1)

```
_::eachReference(list,iterator,context)

```

##### *Examples*

[](#examples-1)

```

```

[Table of contents](#table-of-contents) » [Collection Functions](#collection-functions)

### map

[](#map)

---

***Alias***: collect

***Description***: Produces a new array of values by mapping each value in list through a transformation function (iterator). The iterator is bound to the context object, if one is passed. Each invocation of iterator is called with three arguments: (element, index, list). If list is an object, iterator's arguments will be (value, key, list).

##### *Parameters*

[](#parameters-2)

- *list*: traversable, the list of items to map
- *iterator*: callable, the transformation function
- *context*: object, optional, if provided will become the context of $iterator

##### *Prototype*

[](#prototype-2)

```
_::map(list,iterator,context)

```

##### *Examples*

[](#examples-2)

```

```

[Table of contents](#table-of-contents) » [Collection Functions](#collection-functions)

### reduce

[](#reduce)

---

***Alias***: inject, foldl

***Description***: Also known as inject and foldl, reduce boils down a list of values into a single value. Memo is the initial state of the reduction, and each successive step of it should be returned by iterator. The iterator is passed four arguments: the memo, then the value and index (or key) of the iteration, and finally a reference to the entire list.

##### *Parameters*

[](#parameters-3)

- *list*: traversable, the list of items to reduce
- *iterator*: callable, the reduction function
- *memo*: mixed, The initial reduction state
- *context*: object, optional, if provided will become the context of $iterator

##### *Prototype*

[](#prototype-3)

```
_::reduce(list,iterator,memo,context)

```

##### *Examples*

[](#examples-3)

```

```

[Table of contents](#table-of-contents) » [Collection Functions](#collection-functions)

### reduceRight

[](#reduceright)

---

***Alias***: foldr

***Description***: The right-associative version of reduce.

##### *Parameters*

[](#parameters-4)

- *list*: traversable, the list of items to reduce
- *iterator*: callable, the reduction function
- *memo*: mixed, The initial reduction state
- *context*: object, optional, if provided will become the context of $iterator

##### *Prototype*

[](#prototype-4)

```
_::reduceRight(list,iterator,memo,context)

```

##### *Examples*

[](#examples-4)

```

```

[Table of contents](#table-of-contents) » [Collection Functions](#collection-functions)

### find

[](#find)

---

***Alias***: detect

***Description***: Looks through each value in the list, returning the first one that passes a truth test (iterator), or null if no value passes the test. The function returns as soon as it finds an acceptable element, and doesn't traverse the entire list.

##### *Parameters*

[](#parameters-5)

- *list*: traversable, the list of items to iterate over
- *iterator*: callable, the truth-test function
- *context*: object, optional, if provided will become the context of $iterator

##### *Prototype*

[](#prototype-5)

```
_::find(list,iterator,context)

```

##### *Examples*

[](#examples-5)

```

```

[Table of contents](#table-of-contents) » [Collection Functions](#collection-functions)

### filter

[](#filter)

---

***Alias***: select

***Description***: Looks through each value in the list, returning an array of all the values that pass a truth test (iterator). If iterator isn't provided, each value will be evaluated as a boolean.

##### *Parameters*

[](#parameters-6)

- *list*: traversable, the list of items to filter
- *iterator*: callable, the filtering function
- *context*: object, optional, if provided will become the context of $iterator

##### *Prototype*

[](#prototype-6)

```
_::filter(list,iterator,context)

```

##### *Examples*

[](#examples-6)

```

```

[Table of contents](#table-of-contents) » [Collection Functions](#collection-functions)

### where

[](#where)

---

***Description***: Looks through each value in the list, returning an array of all the values that contain all of the key-value pairs listed in properties.

##### *Parameters*

[](#parameters-7)

- *list*: traversable, the list of items to filter
- *properties*: traversable, the key-values pairs each filtered item must match

##### *Prototype*

[](#prototype-7)

```
_::where(list,properties)

```

##### *Examples*

[](#examples-7)

```
