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

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

minnur/array-query
==================

Array Query

v1.0.4(3y ago)18.0k↓28.6%MITPHP

Since Oct 12Pushed 3y agoCompare

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

READMEChangelog (3)DependenciesVersions (4)Used By (0)

Array Query
===========

[](#array-query)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/8b346ebe1d59fe5b577469d0a6bbb2fdc7c2990b5ddb41a9ed32acc6b434908a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6175726574746f37382f61727261792d71756572792f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mauretto78/array-query/?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/d8f4ecf8f56ae86fd67ad6189ccb57d7938ae996644cec325651bb41b743b0ba/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f62326633343364362d323435392d346236662d623263392d6333336130356134383264312f6d696e692e706e67)](https://insight.sensiolabs.com/projects/b2f343d6-2459-4b6f-b2c9-c33a05a482d1)[![Codacy Badge](https://camo.githubusercontent.com/c845391e38a41f45f0d80a5b3b866e9030d93a183010f48b14217611f970611f/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f6131373233316130343230353438653138326563353835313663643162353632)](https://www.codacy.com/app/mauretto78/array-query?utm_source=github.com&utm_medium=referral&utm_content=mauretto78/array-query&utm_campaign=Badge_Grade)[![Coverage Status](https://camo.githubusercontent.com/424d97e3d0418cb47be2771d942b7846346e014fda9273d9ae219e384d309e8f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d6175726574746f37382f61727261792d71756572792f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/mauretto78/array-query?branch=master)![license](https://camo.githubusercontent.com/0c2661fe53f7a651146652804670d1fa5044e380ed69bfa8c6fb44e6555c5b9d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d6175726574746f37382f61727261792d71756572792e737667)![Packagist](https://camo.githubusercontent.com/0b0a620bc82e9dc789129859e7c1d9318b5b2d4d69baba47dd9d94675b68cc6b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6175726574746f37382f61727261792d71756572792e737667)

**Array Query** allows you to perform queries on multidimensional arrays.

Use Cases
---------

[](#use-cases)

This library is suitable for you if you need to perform some queries on:

- static php arrays
- in-memory stored arrays
- parsed json (or yaml) files

Basic Usage
-----------

[](#basic-usage)

To instantiate the QueryBuilder do the following:

```
use ArrayQuery\QueryBuilder;

$array = [
    [
        'id' => 1,
        'title' => 'Leanne Graham',
        'email' => 'Sincere@april.biz',
        'rate' => 5,
        'company' => [
            'name' => 'Romaguera-Jacobson',
            'catchPhrase' => 'Face to face bifurcated interface',
            'bs' => 'e-enable strategic applications'
        ]
    ],
    [
        'id' => 2,
        'title' => 'Ervin Howell',
        'email' => 'Shanna@melissa.tv',
        'rate' => 3,
        'company' => [
            'name' => 'Robel-Corkery',
            'catchPhrase' => 'Multi-tiered zero tolerance productivity',
            'bs' => 'transition cutting-edge web services'
        ]
    ],
    [
        'id' => 3,
        'title' => 'Clementine Bauch',
        'email' => 'Nathan@yesenia.net',
        'rate' => 4,
        'company' => [
            'name' => 'Keebler LLC',
            'catchPhrase' => 'User-centric fault-tolerant solution',
            'bs' => 'revolutionize end-to-end systems'
        ]
    ],
    // ..
]

QueryBuilder::create($array);

// to add an element to your array. Do this BEFORE make a query on the array
$element = [
   'id' => 4,
   'title' => 'Patricia Lebsack',
   'email' => 'Julianne.OConner@kory.org',
   'rate' => 2,
   'company' => [
       'name' => 'Robel-Corkery',
       'catchPhrase' => 'Multi-tiered zero tolerance productivity',
       'bs' => 'transition cutting-edge web services'
   ]
];
$qb->addElement($element, 4);

// to remove an element from array by his key. Do this BEFORE make a query on the array
$qb->removeElement(3);
```

Data consistency
----------------

[](#data-consistency)

QueryBuilder checks for your data consistency. If an inconsistency is detected a `NotConsistentDataException` will be raised:

```
use ArrayQuery\QueryBuilder;

$array = [
    [
        'id' => 1,
        'title' => 'Leanne Graham',
        'email' => 'Sincere@april.biz',
        'rate' => 5,
        'company' => [
            'name' => 'Romaguera-Jacobson',
            'catchPhrase' => 'Face to face bifurcated interface',
            'bs' => 'e-enable strategic applications'
        ]
    ],
    [
        'id' => 2,
        'title' => 'Ervin Howell',
        'email' => 'Shanna@melissa.tv',
        'rate' => 3,
        'company' => [
            'name' => 'Robel-Corkery',
            'catchPhrase' => 'Multi-tiered zero tolerance productivity',
            'bs' => 'transition cutting-edge web services'
        ]
    ],
    [
        'id' => 3,
        'title' => 'Clementine Bauch',
        'email' => 'Nathan@yesenia.net',
        'rate' => 4,
        'company' => [
            'name' => 'Keebler LLC',
            'catchPhrase' => 'User-centric fault-tolerant solution',
            'bs' => 'revolutionize end-to-end systems'
        ],
        'extra-field' => 'this is an extra field'
    ],
]

// NotConsistentDataException will be raised
QueryBuilder::create($array);
```

Quering, sorting and get results
--------------------------------

[](#quering-sorting-and-get-results)

You can perform queries on your array. You can concatenate criteria:

```
use ArrayQuery\QueryBuilder;

// ..

$qb = QueryBuilder::create($array);
$qb
    ->addCriterion('title', 'Leanne', 'CONTAINS')
    ->addCriterion('rate', '3', '>')
    ->sortedBy('title', 'DESC');

// you can search by nested keys
$qb->addCriterion('company.name', 'Romaguera-Jacobson');

// get results
foreach ($qb->getResults() as $element){
    // ...
}

// get first result
$first = $qb->getFirstResult();

// get last result
$last = $qb->getLastResult();

// get a result by index
$thirdResult = $qb->getResult(3);
```

### Avaliable criteria operators

[](#avaliable-criteria-operators)

- `=` (default operator, can be omitted)
- `>`
- ` 1,
        'name' => 'Mauro Cassani',
        'id_category' => 3,
        'email' => 'assistenza@easy-grafica.com'
    ],[
        'id' => 2,
        'name' => 'Mario Rossi',
        'id_category' => 3,
        'email' => 'mario.rossi@gmail.com'
    ],[
        'id' => 3,
        'name' => 'Maria Bianchi',
        'id_category' => 1,
        'email' => 'maria.bianchi@gmail.com'
    ]
];
$category = [
    'id' => 3,
    'name' => 'Web Developer'
];

$qb = QueryBuilder::create($users)
    ->join($category, 'category', 'id_category', 'id')
    ->addCriterion('category.id', 3);

foreach ($qb->getResults() as $element){
    // ...
}
```

Limit and Offset
----------------

[](#limit-and-offset)

You can add criteria and specify limit and offset for your query results:

```
use ArrayQuery\QueryBuilder;

$qb = QueryBuilder::create($array);
$qb
    ->addCriterion('title', ['Leanne'], 'IN_ARRAY')
    ->addCriterion('rate', '3', '>')
    ->sortedBy('title')
    ->limit(0, 10);

foreach ($qb->getResults() as $element){
    // ...
}
```

Working with dates
------------------

[](#working-with-dates)

You can perform queries based on datetime fields. You can use `DATE_ASC` or `DATE_DESC` operator to sort results by date. You must specify **date format** if your format is not `YYYY-mm-dd`:

```
use ArrayQuery\QueryBuilder;

$qb = QueryBuilder::create($array);
$qb
    ->addCriterion('registration_date', '01/05/2017', 'GT_DATE', 'd/m/Y')
    ->addCriterion('rate', '3', '>')
    ->sortedBy('registration_date', 'DATE_DESC', 'd/m/Y')
    ->limit(0, 10);

foreach ($qb->getResults() as $element){
    // ...
}
```

Aliases
-------

[](#aliases)

You can use aliases by using the `as` keyword as a delimiter. Do the following:

```
use ArrayQuery\QueryBuilder;

$qb = QueryBuilder::create($array);
$qb
    ->addCriterion('name as n', 'Ervin Howell')
    ->addCriterion('username as user', 'Antonette')
    ->addCriterion('address.street as street', 'Victor Plains');

foreach ($qb->getResults() as $element){
    // ...
    // now you have
    // $element['n']
    // $element['user']
    // $element['street']
}
```

Shuffled results
----------------

[](#shuffled-results)

You can shuffle query results by using `getShuffledResults` method:

```
use ArrayQuery\QueryBuilder;

$qb = QueryBuilder::create($array);

foreach ($qb->getShuffledResults() as $element){
    // ...
}
```

More examples
-------------

[](#more-examples)

Please refer to [QueryBuilderTest](https://github.com/mauretto78/array-query/blob/master/tests/QueryBuilderTest.php) for more examples.

Support
-------

[](#support)

If you found an issue or had an idea please refer [to this section](https://github.com/mauretto78/array-query/issues).

Authors
-------

[](#authors)

- **Mauro Cassani** - [github](https://github.com/mauretto78)

License
-------

[](#license)

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 75% 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 ~2 days

Total

3

Last Release

1310d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/27d64866885eb274531503f8cff7b4cc6d9a1ff7a2dd03d3b3182ccbb549b967?d=identicon)[minnur](/maintainers/minnur)

---

Top Contributors

[![minnur](https://avatars.githubusercontent.com/u/4031934?v=4)](https://github.com/minnur "minnur (6 commits)")[![mauretto78](https://avatars.githubusercontent.com/u/10035321?v=4)](https://github.com/mauretto78 "mauretto78 (2 commits)")

---

Tags

phparrayquery

### Embed Badge

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

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

###  Alternatives

[nahid/qarray

QArray is a PHP abstraction for querying array

108403.2k2](/packages/nahid-qarray)[zakirullin/mess

Convenient array-related routine &amp; better type casting

21228.9k2](/packages/zakirullin-mess)[dotty/dotty

Easy access to array data using dot notation

1293.8k1](/packages/dotty-dotty)

PHPackages © 2026

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