Array Query allows you to perform queries on multidimensional arrays.
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
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);
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){
// ...
}
=
(default operator, can be omitted)>
<
<=
>=
!=
GT_DATE
GTE_DATE
LT_DATE
LTE_DATE
EQUALS_DATE
IN_ARRAY
IN_ARRAY_INVERSED
ARRAY_MATCH
CONTAINS
(case insensitive)
ASC
(default operator, can be omitted)DESC
DATE_ASC
DATE_DESC
You can add criteria and specify limit and offset for your query results:
use ArrayQuery\QueryBuilder;
$qb = QueryBuilder::create($array);
$qb
->addCriterion('title', ['Leanne'], 'ARRAY')
->addCriterion('rate', '3', '>')
->sortedBy('title')
->limit(0, 10);
foreach ($qb->getResults() as $element){
// ...
}
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){
// ...
}
If you found an issue or had an idea please refer to this section.
- Mauro Cassani - github
This project is licensed under the MIT License - see the LICENSE.md file for details