PHPackages                             rmtram/array-query - 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. rmtram/array-query

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

rmtram/array-query
==================

Array query

v1.0.0(6y ago)320[1 PRs](https://github.com/Rmtram/ArrayQuery/pulls)MITPHPPHP &gt;=7.2CI failing

Since Nov 27Pushed 3mo ago3 watchersCompare

[ Source](https://github.com/Rmtram/ArrayQuery)[ Packagist](https://packagist.org/packages/rmtram/array-query)[ Docs](https://github.com/Rmtram/ArrayQuery)[ RSS](/packages/rmtram-array-query/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (2)Dependencies (2)Versions (5)Used By (0)

[![Build Status](https://camo.githubusercontent.com/fcddf477d9a55f83e90e9e471ae648fd1d9bf9a1827b67fb5c5c05280451f855/68747470733a2f2f7472617669732d63692e6f72672f526d7472616d2f417272617951756572792e737667)](https://travis-ci.org/Rmtram/ArrayQuery)[![Latest Stable Version](https://camo.githubusercontent.com/c75123c8c51c62b73729758825e914cb2ddecc4b07500de1e0bfece4a7e1b350/68747470733a2f2f706f7365722e707567782e6f72672f726d7472616d2f61727261792d71756572792f762f737461626c65)](https://packagist.org/packages/rmtram/array-query)[![Total Downloads](https://camo.githubusercontent.com/5e1a22f4d58a43bfb83d51a83bf8386d97c1ca624cfc1147bf64d7fbf205d8d3/68747470733a2f2f706f7365722e707567782e6f72672f726d7472616d2f61727261792d71756572792f646f776e6c6f616473)](https://packagist.org/packages/rmtram/array-query)[![License](https://camo.githubusercontent.com/97d46034a291b9dc59450614d48c028941b7355164ac2b6f6de5da5d76b8b89c/68747470733a2f2f706f7365722e707567782e6f72672f726d7472616d2f61727261792d71756572792f6c6963656e7365)](https://packagist.org/packages/rmtram/array-query)

ArrayQuery
==========

[](#arrayquery)

This library provides ORM-like Array filtering.

Install
-------

[](#install)

```
$ composer require rmtram/array-query

```

Usage
=====

[](#usage)

```
use Rmtram\ArrayQuery\ArrayQuery;
use Rmtram\ArrayQuery\Queries\Where;

$aq = new ArrayQuery([
    [
        'id' => 1,
        'name' => 'hoge',
        'blog' => [
            'title' => 'hoge blog',
            'category' => 'programming',
            'url'   => '#hoge',
            'end_date' => '2010-10-10',
        ]
    ],
    [
        'id' => 2,
        'name' => 'fuga',
        'blog' => [
            'title' => 'fuga blog',
            'category' => 'anime',
            'url'   => '#fuga',
            'end_date' => null
        ]
    ],
        [
        'id' => 3,
        'name' => 'piyo',
        'blog' => [
            'title' => 'piyo blog',
            'category' => 'anime',
            'url'   => '#piyo',
            'end_date' => '2010-10-14',

        ]
    ],
]);

$results = $aq->in('blog.category', ['anime', 'programming'])
    ->and(function (Where $where) {
        $where->eq('blog.end_date', '2010-10-10')->or(function (Where $where) {
            $where->null('blog.end_date');
        });
    })
    ->all(); // [['id' => 1, ...], ['id' => 2, ...]]
```

Contents
========

[](#contents)

- [Initializations](#Initializations)
    - [constructor](#constructor)
    - [reset](#reset)
- [Operations](#Operations)
    - [eq](#eq)
    - [notEq](#notEq)
    - [in](#in)
    - [notIn](#notIn)
    - [null](#null)
    - [notNull](#notNull)
    - [like](#like)
    - [notLike](#notLike)
    - [gt](#gt)
    - [gte](#gte)
    - [lt](#lt)
    - [lte](#lte)
    - [and](#and)
    - [or](#or)
- [Executions](#Executions)
    - [generator](#generator)
    - [all](#all)
    - [first](#first)
    - [last](#last)
    - [count](#count)
    - [exists](#exists)
    - [map](#map)
    - [pluck](#pluck)
    - [pluckFirst](#pluckFirst)
    - [pluckLast](#pluckLast)
- [Setters](#Setters)
    - [setDelimiter](#setDelimiter)
    - [setResettable](#setResettable)

Methods
=======

[](#methods)

Initializations
---------------

[](#initializations)

### constructor

[](#constructor)

> Definition

```
constructor(array $items, bool $resettable = true)

```

> Source code

```
// resettable = true (default)
$aq = new Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->eq('id', 1)->count(); // 1

// state: eq('id', 2)
$aq->eq('id', 2)->count(); // 1

// resettable = false
$aq = new Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
], false);

$aq->eq('id', 1)->count(); // 1

// state: eq('id', 1)->eq('id', 2)
$aq->eq('id', 2)->count(); // 0
```

### reset

[](#reset)

> Definition

```
reset(): self

```

> Source code

```
// resettable = false
$aq = new Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
], false);

$aq->eq('id', 1)->count(); // 1

// state: eq('id', 1)->eq('id', 2)
$aq->eq('id', 2)->count(); // 0

// state: eq('id', 2)
$aq->reset()->eq('id', 2)->count(); // 1
```

Operations
----------

[](#operations)

### eq

[](#eq)

> Definition

```
eq(string $key, mixed $val): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 18],
]);
$aq->eq('id', 1)->all(); // [['id' => 1, 'age' => 18]]
$aq->eq('id', 1)->eq('age', 18)->all(); // [['id' => 1, 'age' => 18]]
$aq->eq('id', -1)->all(); // []
```

### notEq

[](#noteq)

> Definition

```
notEq(string $key, mixed $val): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 18],
    ['id' => 2, 'age' => 18],
]);
$aq->notEq('id', 1)->all(); // [['id' => 2, 'age' => 18]]
$aq->eq('id', 1)->notEq('age', 18)->all(); // []
$aq->notEq('id', -1)->all(); // [['id' => 1, 'age' => 18], ['id' => 2, 'age' => 18]]
```

### in

[](#in)

> Definition

```
in(string $key, array $val): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 18],
    ['id' => 2, 'age' => 18],
]);
$aq->in('id', [1])->all(); // [['id' => 1, 'age' => 18]]
$aq->in('id', [1, 2])->all(); // [['id' => 1, 'age' => 18], ['id' => 2, 'age' => 18]]
$aq->in('id', [2, 3])->all(); // [['id' => 2, 'age' => 18]]
$aq->in('id', [-1])->all(); // []
```

### notIn

[](#notin)

> Definition

```
notIn(string $key, array $val): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 18],
    ['id' => 2, 'age' => 18],
]);
$aq->notIn('id', [1])->all(); // [['id' => 2, 'age' => 18]]
$aq->notIn('id', [1, 2])->all(); // []
$aq->notIn('id', [2, 3])->all(); // [['id' => 1, 'age' => 18]]
$aq->notIn('id', [-1])->all(); // [['id' => 1, 'age' => 18], ['id' => 2, 'age' => 18]]
```

### null

[](#null)

> Definition

```
null(string $key, bool $checkExistsKey = false): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'address' => null],
    ['id' => 2],
    ['id' => 3, 'address' => 'x'],
]);

$aq->null('address')->all(); // [['id' => 1, 'address' => null], ['id' => 2]]
$aq->null('address', true)->all(); // [['id' => 1, 'address' => null]]
```

### notNull

[](#notnull)

> Definition

```
notNull(string $key): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'address' => null],
    ['id' => 2],
    ['id' => 3, 'address' => 'x'],
]);

$aq->notNull('address')->all(); // [['id' => 3, 'address' => 'x']]
```

### like

[](#like)

> Definition

```
like(string $key, string $val): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'name' => 'hoge'],
    ['id' => 2, 'name' => 'fuga'],
]);
$aq->like('name', 'h%')->all(); // [['id' => 1, 'name' => 'hoge']]
$aq->like('name', '%g%')->all(); // [['id' => 1, 'name' => 'hoge'], ['id' => 2, 'name' => 'fuga']]
$aq->like('name', 'nothing')->all(); // []
```

### notLike

[](#notlike)

> Definition

```
notLike(string $key, string $val): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'name' => 'hoge'],
    ['id' => 2, 'name' => 'fuga'],
]);
$aq->notLike('name', 'h%')->all(); // [['id' => 2, 'name' => 'fuga']]
$aq->notLike('name', '%g%')->all(); // []
$aq->notLike('name', 'nothing')->all(); // [['id' => 1, 'name' => 'hoge'], ['id' => 2, 'name' => 'fuga']]
```

### gt

[](#gt)

`gt` is an alias for `greater than`, compare `a > b`.

> Definition

```
gt(string $key, mixed $val): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 15],
    ['id' => 2, 'age' => 16],
    ['id' => 3, 'age' => 17],
]);

$aq->gt('age', 14)->all(); // [['id' => 2, 'age' => 16], ['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]
$aq->gt('age', 15)->all(); // [['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]
$aq->gt('age', 16)->all(); // [['id' => 3, 'age' => 17]]
$aq->gt('age', 17)->all(); // []
```

### gte

[](#gte)

`gte` is an alias for `greater equal than`, compare `a >= b`.

> Definition

```
gte(string $key, mixed $val): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 15],
    ['id' => 2, 'age' => 16],
    ['id' => 3, 'age' => 17],
]);

$aq->gte('age', 15)->all(); // [['id' => 2, 'age' => 16], ['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]
$aq->gte('age', 16)->all(); // [['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]
$aq->gte('age', 17)->all(); // [['id' => 3, 'age' => 17]]
$aq->gte('age', 18)->all(); // []
```

### lt

[](#lt)

`lt` is an alias for `less than`, compare `a < b`.

> Definition

```
lt(string $key, mixed $val): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 15],
    ['id' => 2, 'age' => 16],
    ['id' => 3, 'age' => 17],
]);

$aq->lt('age', 15)->all(); // []
$aq->lt('age', 16)->all(); // [['id' => 3, 'age' => 17]]
$aq->lt('age', 17)->all(); // [['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]
$aq->lt('age', 18)->all(); // [['id' => 2, 'age' => 16], ['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]
```

### lte

[](#lte)

`lte` is an alias for `less equal than`, compare `a < b`.

> Definition

```
lte(string $key, mixed $val): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 15],
    ['id' => 2, 'age' => 16],
    ['id' => 3, 'age' => 17],
]);

$aq->lte('age', 14)->all(); // []
$aq->lte('age', 15)->all(); // [['id' => 3, 'age' => 17]]
$aq->lte('age', 16)->all(); // [['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]
$aq->lte('age', 17)->all(); // [['id' => 2, 'age' => 16], ['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]
```

### and

[](#and)

> Definition

```
and(callable $callback(\Rmtram\ArrayQuery\Queries\Where $where)): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'status' => 'active', 'age' => 18],
    ['id' => 2, 'status' => 'active', 'age' => 20],
    ['id' => 3, 'status' => 'active', 'age' => 19],
]);
$aq->eq('status', 'active')
    ->and(function (\Rmtram\ArrayQuery\Queries\Where $where) {
        $where->eq('id', 1)->or(function (\Rmtram\ArrayQuery\Queries\Where $where) {
            $where->eq('age', 19);
        });
    })->all();
    // [
    //     ['id' => 1, 'status' => 'active', 'age' => 18],
    //     ['id' => 3, 'status' => 'active', 'age' => 19]
    // ]
```

### or

[](#or)

> Definition

```
or(callable $callback(\Rmtram\ArrayQuery\Queries\Where $where)): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'status' => 'active', 'age' => 18],
    ['id' => 2, 'status' => 'active', 'age' => 20],
    ['id' => 3, 'status' => 'active', 'age' => 19],
]);
$aq->eq('id', 1)
    ->or(function (\Rmtram\ArrayQuery\Queries\Where $where) {
        $where->eq('age', 20);
    })->all();
    // [
    //     ['id' => 1, 'status' => 'active', 'age' => 18],
    //     ['id' => 2, 'status' => 'active', 'age' => 20]
    // ]
```

Executions
----------

[](#executions)

### generator

[](#generator)

> Definition

```
generator(): Generator

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$generator = $aq->eq('id', 1)->generator();

// (Generator)[['id' => 1]]
foreach ($generator as $item) {
    echo $item['id']; // 1
}
```

### all

[](#all)

> Definition

```
all(): array

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->all(); // (array)[['id' => 1], ['id' => 2]]
$aq->eq('id', 2)->all(); // (array)[['id' => 2]]
```

### first

[](#first)

> Definition

```
first(): ?array

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->first(); // (array)['id' => 1]
$aq->eq('id', 2)->first(); // (array)['id' => 2]
$aq->eq('id', 3)->first(); // null
```

### last

[](#last)

> Definition

```
last(): ?array

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->last();  // (array)['id' => 2]
$aq->eq('id', 3)->last();  // null
```

### count

[](#count)

> Definition

```
count(): int

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->count(); // 2
$aq->eq('id', 2)->count(); // 1
$aq->eq('id', 3)->count(); // 0
```

### exists

[](#exists)

> Definition

```
exists(): bool

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->exists(); // true
$aq->eq('id', 2)->exists(); // true
$aq->eq('id', 3)->exists(); // false
```

### map

[](#map)

> Definition

```
map(callable $callback(array $item)): array

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->map(function(array $item) {
    return $item['id'];
}); // [1, 2]

$aq->eq('id', 1)->map(function(array $item) {
    return $item['id'];
}); // [1]

$aq->eq('id', 3)->map(function(array $item) {
    return $item['id'];
}); // []
```

### pluck

[](#pluck)

> Definition

```
pluck(array $keys): array

```

> Source code

```
$aq = new Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 1, 'address' => 'a'],
    ['id' => 2, 'age' => 2, 'address' => 'b'],
    ['id' => 3, 'age' => 3, 'address' => 'c'],
]);

$aq->pluck(['id', 'age']); // [['id' => 1, 'age' => 1], ['id' => 2, 'age' => 2], ['id' => 3, 'age' => 3]]
```

### pluckFirst

[](#pluckfirst)

> Definition

```
pluckFirst(array $keys): ?array

```

> Source code

```
$aq = new Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 1, 'address' => 'a'],
    ['id' => 2, 'age' => 2, 'address' => 'b'],
    ['id' => 3, 'age' => 3, 'address' => 'c'],
]);

$aq->pluckFirst(['id', 'age']); // ['id' => 1, 'age' => 1]
$aq->eq('id', -1)->pluckFirst(['id', 'age']); // null
```

### pluckLast

[](#plucklast)

> Definition

```
pluckLast(array $keys): ?array

```

> Source code

```
$aq = new Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 1, 'address' => 'a'],
    ['id' => 2, 'age' => 2, 'address' => 'b'],
    ['id' => 3, 'age' => 3, 'address' => 'c'],
]);

$aq->pluckLast(['id', 'age']); // ['id' => 3, 'age' => 3]
$aq->eq('id', -1)->pluckFirst(['id', 'age']); // null
```

Setters
-------

[](#setters)

### setDelimiter

[](#setdelimiter)

> Definition

```
setDelimiter(string $delimiter): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'options' => ['address' => 'x']],
    ['id' => 2],
]);

$aq->eq('options.address', 'x')->exists(); // true

$aq->setDelimiter('@');
$aq->eq('options@address', 'x'); // true;
```

### setResettable

[](#setresettable)

> Definition

```
setResettable(bool $resettable): self

```

> Source code

```
$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->eq('id', 1)->count(); // 1

// state: eq('id', 2)
$aq->eq('id', 2)->count(); // 1

$aq->setResettable(false);

$aq->eq('id', 1)->count(); // 1

// state: eq('id', 1)->eq('id', 2)
$aq->eq('id', 2)->count(); // 0
```

Support versions
================

[](#support-versions)

- 7.2
- 7.3
- 7.4

LICENSE
=======

[](#license)

MIT

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance53

Moderate activity, may be stable

Popularity10

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 82.5% 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 ~560 days

Total

3

Last Release

2337d ago

Major Versions

v0.0.1 → 1.0.x-dev2019-12-21

### Community

Maintainers

![](https://www.gravatar.com/avatar/8d7cbad47788700bb6b0fe45e8431c751d6be5d4cb7806895140c9e38f3bd7b3?d=identicon)[Rmtram](/maintainers/Rmtram)

---

Top Contributors

[![Rmtram](https://avatars.githubusercontent.com/u/15045107?v=4)](https://github.com/Rmtram "Rmtram (66 commits)")[![NoguHiro](https://avatars.githubusercontent.com/u/4655005?v=4)](https://github.com/NoguHiro "NoguHiro (14 commits)")

---

Tags

arrayqueryfilterarray queryarray-filter

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/rmtram-array-query/health.svg)

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

###  Alternatives

[symfony/property-access

Provides functions to read and write from/to an object or array using a simple string notation

2.8k295.3M2.5k](/packages/symfony-property-access)[league/config

Define configuration arrays with strict schemas and access values with dot notation

564302.2M24](/packages/league-config)[athari/yalinqo

YaLinqo, a LINQ-to-objects library for PHP

4561.2M5](/packages/athari-yalinqo)[league/uri-components

URI components manipulation library

31932.3M67](/packages/league-uri-components)[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)

PHPackages © 2026

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