PHPackages                             rikta/php-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. rikta/php-query

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

rikta/php-query
===============

Fluently query a repository for data

0.1.3(4y ago)01MITPHPPHP ^7.4|^8.0CI failing

Since Sep 28Pushed 4y ago1 watchersCompare

[ Source](https://github.com/RiktaD/php-query)[ Packagist](https://packagist.org/packages/rikta/php-query)[ RSS](/packages/rikta-php-query/feed)WikiDiscussions main Synced 1mo ago

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

PhpQuery
========

[](#phpquery)

[![packagist name](https://camo.githubusercontent.com/f5cf12b23d861ea865127bd24ac78369f01f436fc7e8b243cfd9175f8f3565a1/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f6e616d652f72696b74612f7068702d7175657279)](https://packagist.org/packages/rikta/php-query)[![version](https://camo.githubusercontent.com/e1a234a4ba8e80d4a82c82059601ab8312a5e5e01a4af53cee2468c92eb62ff6/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f762f72696b74612f7068702d71756572792f6c61746573743f6c6162656c26636f6c6f723d677265656e)](https://github.com/RiktaD/php-query/releases)[![php version](https://camo.githubusercontent.com/ec9fd5aadfe4effed1c75eb1415fe23bace4fec4d2b63f954298e6ab44c6fe64/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f7068702f72696b74612f7068702d7175657279)](https://github.com/RiktaD/php-query/blob/main/composer.json)

[![license](https://camo.githubusercontent.com/feac18f9ebbb5b0d0b7204323b2828017b82b7fc218ec2fde053b33b6745117c/68747470733a2f2f62616467656e2e6e65742f6769746875622f6c6963656e73652f72696b7461642f7068702d7175657279)](https://github.com/RiktaD/php-query/blob/main/LICENSE.md)[![GitHub commit activity](https://camo.githubusercontent.com/c1bf5372b0aa38971db7c7c935c95d1d558b17dbd85300e9c905aee7de2c8e36/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6d6d69742d61637469766974792f6d2f72696b7461642f7068702d7175657279)](https://github.com/RiktaD/php-query/graphs/commit-activity)[![open issues](https://camo.githubusercontent.com/d1fbd3c961ba0c1da73e78fc83c0f0ad03f833c5d656eb88176aa94bcf7177f2/68747470733a2f2f62616467656e2e6e65742f6769746875622f6f70656e2d6973737565732f72696b7461642f7068702d7175657279)](https://github.com/RiktaD/php-query/issues?q=is%3Aopen+is%3Aissue)[![closed issues](https://camo.githubusercontent.com/3d3e7ccb73321ee309f779dbad0abc868aa1fd0a0fd47559241703f00a6e6b95/68747470733a2f2f62616467656e2e6e65742f6769746875622f636c6f7365642d6973737565732f72696b7461642f7068702d7175657279)](https://github.com/RiktaD/php-query/issues?q=is%3Aissue+is%3Aclosed)

[![ci](https://camo.githubusercontent.com/c5066b687f47542422c274e56cc449c2062c706c0ce4b180911c6a2813bc2b7b/68747470733a2f2f62616467656e2e6e65742f6769746875622f636865636b732f72696b7461642f7068702d71756572793f6c6162656c3d6369)](https://github.com/RiktaD/php-query/actions?query=branch%3Amain+workflow%3A%22Testing+Query%22+workflow%3Acreate-release++)[![dependabot](https://camo.githubusercontent.com/413496429895c9767178c1b42c8b3d5638de47f96199ad68286ad13d7c372c10/68747470733a2f2f62616467656e2e6e65742f6769746875622f646570656e6461626f742f72696b7461642f7068702d7175657279)](https://dependabot.com)[![maintainability score](https://camo.githubusercontent.com/6e4c01befc96442f1f493cb857ae28bf4d0253dcca0bc72af557803a86a3c569/68747470733a2f2f62616467656e2e6e65742f636f6465636c696d6174652f6d61696e7461696e6162696c6974792f52696b7461442f7068702d7175657279)](https://codeclimate.com/github/RiktaD/php-query)[![tech debt %](https://camo.githubusercontent.com/387180b5f4e29bfa8c9277ab4716c44e2061dcb5a62e9550ca824f4a63a36a88/68747470733a2f2f62616467656e2e6e65742f636f6465636c696d6174652f746563682d646562742f52696b7461442f7068702d7175657279)](https://codeclimate.com/github/RiktaD/php-query/issues)[![maintainability issues](https://camo.githubusercontent.com/c3c9a6aca900cde58f6043955fbdabc9ecf6664a4123313c1462512c50b8e2ab/68747470733a2f2f62616467656e2e6e65742f636f6465636c696d6174652f6973737565732f52696b7461442f7068702d71756572793f6c6162656c3d6d61696e7461696e6162696c697479253230697373756573)](https://codeclimate.com/github/RiktaD/php-query/issues)

Fluently Query a repository or array for items that match certain criteria and arrange the results in a particular order.

Like a low-budget php version of an ORM-library where budget wasn't even enough for a database and we have to be happy with in-session-memory.

Contrary to the proper db-based libraries this one works on anything that can be represented inside a key-value store or array.

Just wrap a collection of something (Arrays, Files in a Directory, a decoded CSV-File) into a [rikta/repository](https://packagist.org/packages/rikta/repository) and fire a query on it.

You could also store the query on a variable and then call it for different repositories, or use it as a configurable getter.

*(For your convenience you can also just pass an array; it will be converted into an `ArrayRepository` automatically)*

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

[](#installation)

`composer require rikta/php-query`

No config, no dependency-injection, nothing. Plug&amp;Play!

Usage
-----

[](#usage)

1. Create a new query object, e.g. `$query = new Query();`
2. Add some operations, e.g. `$query->not()->isNull()->sort()->limit(10)`
3. Get your result-object, e.g. `$result = $query->getResults()`
4. Get your items from the result, e.g. `return $result->toArray()` (with keys) or `return $result->getItems()` (without keys)

### Example

[](#example)

The `examples`-directory contains several examples (those are part of a testsuite and therefore guaranteed to work)

One of the rather verbose ones, it uses a list of 100 popular classical books as input and finds the three biggest english books after 1900 mapped to their authors

```
$query = (new \Rikta\PhpQuery\Query())
    ->onPathValue('.year')->greaterThanOrEqual(1900) // only books after 1900
    ->onPathValue('.language')->identical('English') // only books in english
    ->onPathValue('.pages')->sort(static fn ($a, $b) => $b  $a) // sort by pages, in descending order
    ->limit(3) // limit the results to three
    ->onPathValue('.title')->mapToKey() // set the key to $value['title']
    ->onPathValue('.author')->mapToValue() // set the value to $value['author']
    ->sort();

$results = $query
    ->getResultsFor(new \Rikta\PhpQuery\Examples\_Data\BookDataRepository())
    ->toArray();

\PHPUnit\Framework\assertEquals([
            'The Golden Notebook' => 'Doris Lessing',
            'Tales' => 'Edgar Allan Poe',
            'Invisible Man' => 'Ralph Ellison',
        ], $results);
```

Operations
----------

[](#operations)

Operations are invokable objects (= callables with a state, you could say) that

- get all the data they need at construction time, maybe even other operations
- get an array of items at invocation
- do something with the passed $items
- return it for the next operation to modify

Operations are divided into multiple categories. Every operation will retain the initial keys in its return (unless specifically instructed otherwise).

### QueryOperation Categories:

[](#queryoperation-categories)

#### Filter

[](#filter)

Filters the keys/values, e.g. against an array of a callable

Check [\_FilterOperationMethodsInterface](src/Operation/Filter/_FilterOperationMethodsInterface.php) for an up-to-date overview of all implemented filter-methods

**Examples:**

- only show keys from provided array
- only show value which is bigger than two

#### Juggling

[](#juggling)

Modifies the arrays order and bounds, but not the values themselves (Except maybe discarding them)

Check [\_JugglingOperationMethodsInterface](src/Operation/Juggling/_JugglingOperationMethodsInterface.php) for an up-to-date overview of all implemented filter-methods

**Examples:**

- sort in ascending order of value
- sort in descending order of keys
- limit to 20 results

#### Modification

[](#modification)

Modifies another Operation

Check [\_ModificationOperationMethodsInterface](src/Operation/Modification/_ModificationOperationMethodsInterface.php) for an up-to-date overview of all implemented filter-methods

**Examples:**

- inverting a filter (keep what would have been thrown away and vice versa)
- comparing a subvalue instead of a value

#### Replacement

[](#replacement)

Replaces the keys/values, e.g. with a value from a path or with the result of a callable

Check [\_ReplacementOperationMethodsInterface](src/Operation/Replacement/_ReplacementOperationMethodsInterface.php) for an up-to-date overview of all implemented filter-methods

**Examples:**

- replace the returned values with $value\['something'\]\[0\]
- replace the keys in the result with each valued $value\['id'\]

Why the name?
-------------

[](#why-the-name)

The initial name was just "Query", but during development I ended up with Namespaces like `Rikta\Query\Query\Query`, so I renamed the project to mitigate this redundancy a bit.

I'm open for any suggestions on a better name ;)

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 85% 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

4

Last Release

1692d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f0ccaa2dd19d65140583addf5e5a4178d77932366733e99eeb78cc0fc53c291f?d=identicon)[kuroishinjitsu](/maintainers/kuroishinjitsu)

---

Top Contributors

[![MadWillow](https://avatars.githubusercontent.com/u/16151453?v=4)](https://github.com/MadWillow "MadWillow (17 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (3 commits)")

---

Tags

php-libraryquery-builder

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/rikta-php-query/health.svg)

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

###  Alternatives

[mrsuh/php-bison-skeleton

PHP skeleton for Bison

3810.4k1](/packages/mrsuh-php-bison-skeleton)

PHPackages © 2026

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