PHPackages                             datashaman/array-filter - 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. datashaman/array-filter

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

datashaman/array-filter
=======================

Match an array against a filter

0.1.1(11y ago)226MITPHPPHP &gt;=5.3.0

Since Jul 11Pushed 6y ago1 watchersCompare

[ Source](https://github.com/datashaman/array-filter)[ Packagist](https://packagist.org/packages/datashaman/array-filter)[ Docs](http://github.com/datashaman/array-filter)[ RSS](/packages/datashaman-array-filter/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependencies (2)Versions (4)Used By (0)

Array Filter
============

[](#array-filter)

A literal port of the excellent Javascript library [ mmckegg / json-filter ](https://github.com/mmckegg/json-filter) to PHP.

Credit to [ Matt McKegg ](https://github.com/mmckegg) for creating an incredibly useful, well-tested piece of software.

Match PHP arrays against filters.

[![Build Status](https://camo.githubusercontent.com/a3ed040b1fdb1f6132b1c43a26e62f196b58bedc2e2c6ab6c69ea451ac08ee8a/68747470733a2f2f7472617669732d63692e6f72672f646174617368616d616e2f61727261792d66696c7465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/datashaman/array-filter)

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

[](#installation)

```
$ composer require datashaman/array-filter
```

Filters
-------

[](#filters)

Filters are just arrays that have the keys and values you want your final array to have. e.g. if you wanted to require that the field `type` was always `person` your filter would be `{type: 'person'}`.

If things aren't so black and white, the following conditionals are available:

#### $present

[](#present)

Specify that the value must not be null or false (i.e. 'truthy').

```
array(
  'name' => array( '$present' => true )
)
```

#### $any

[](#any)

Specify that the value can be anything. Useful when matching all keys.

```
array(
  'description' => array( '$any' => true )
)
```

#### $contains

[](#contains)

For matching against an array. The array must contain all of the values specified.

```
array(
  'tags' => array( '$contains' => array( 'cat', 'animal' ) )
)
```

#### $excludes

[](#excludes)

For matching against an array. The array cannot contain any of the values specified.

```
array(
  'permissions' => array( '$excludes' => array( 'admin', 'mod' ) )
)
```

#### $only

[](#only)

The value can only be one of the ones specified.

```
array(
  'gender' => array( '$only' => array( 'male', 'female', 'unknown' ) )
)
```

#### $not

[](#not)

The value can be anything except one of the ones specified.

```
array(
  'browser' => array( '$not' => array( 'IE', 'Firefox' ) )
)
```

#### $matchAny

[](#matchany)

Allows a filter to branch into multiple filters when at least one must match.

```
array(
  '$matchAny' => array(
    array( 'type' => "Post",
      'state' => array( '$only' => array( 'draft', 'published' ) )
    ),
    array( 'type' => "Comment",
      'state' => array( '$only' => array( 'pending', 'approved', 'spam' ) )
    )
  )
)
```

#### $query

[](#query)

Specify a query to get the value to match. Uses `options.queryHandler`.

```
array(
  'type' => 'item',
  'user_id' => array( '$query' => 'user.id' )
)
```

#### $optional

[](#optional)

A shortcut for specifying a lot of $any filters at the same time.

```
array(
  '$optional' => array( 'description', 'color', 'age' )
)
```

Is equivalent to:

```
array(
  'description' => array( '$any' => true ),
  'color' => array( '$any' => true ),
  'age' => array( '$any' => true )
)
```

API
---

[](#api)

```
use DataShaman\ArrayFilter;

$filter = new ArrayFilter;
$filter->checkFilter($source, $filter, $options);
```

### checkFilter(source, filter, options)

[](#checkfiltersource-filter-options)

#### options:

[](#options)

- **match**: specify: 'filter', 'source', 'any', 'all'
    - filter: every filter permission must be satisfied (i.e. required fields)
    - source: every source key must be specified in filter
    - any: the keys don't matter, but if there is a match, they must pass
    - all: all keys must be exactly the same, otherwise fails - for finding changed items - no $conditionals work in this mode
- **queryHandler**: Accepts a function(query, localContext) that returns resulting value
- **context**: Array to pass to the query handler

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

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

Total

2

Last Release

4328d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/48372fbab9c5059c6d5773bb3d42dff0382443ceb65d008fc219ad38a383eafd?d=identicon)[datashaman](/maintainers/datashaman)

---

Top Contributors

[![datashaman](https://avatars.githubusercontent.com/u/59514?v=4)](https://github.com/datashaman "datashaman (35 commits)")

---

Tags

validationarrayqueryfilter

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/datashaman-array-filter/health.svg)

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

###  Alternatives

[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k394.3M1.5k](/packages/nette-utils)[athari/yalinqo

YaLinqo, a LINQ-to-objects library for PHP

4561.2M5](/packages/athari-yalinqo)[nahid/qarray

QArray is a PHP abstraction for querying array

108403.2k2](/packages/nahid-qarray)[rpnzl/arrch

Array queries for PHP 5.3

37134.6k2](/packages/rpnzl-arrch)[lezhnev74/pasvl

Array Validator (regular expressions for nested array, sort of)

5253.7k3](/packages/lezhnev74-pasvl)[ptrofimov/matchmaker

Ultra-fresh PHP matching functions

27167.7k](/packages/ptrofimov-matchmaker)

PHPackages © 2026

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