Is there a query language for filtering an array?

I was wondering if there is a library that implements an SQL-like interface for accessing data in an array, for example.

Input:

[
    ['name' => 'Tom', 'age' => 27, 'location' => ['country' => 'GB']],
    ['name' => 'Jerry', 'age' => 16, 'location' => ['country' => 'LT']],
    ['name' => 'Stuart', 'age' => 26, 'location' => ['country' => 'GB']]
]

Fictional request:

SELECT name, location.country FROM {input} WHERE age > 18 ORDER BY age DESC

This will result in a change:

[
    ['name' => 'Tom', 'location.country' => 'GB'],
    ['name' => 'Tom', 'location.country' => 'GB']
]

Notice that I know very array_filterwell similar implementations that I could put together. I am looking for a query as an interface for accessing data.

+4
source share
3 answers

You can use LINQ with PHP implementations like phpLinq or LINQ for PHP or plinq

+2
source

I found the following closest of all available alternatives:

https://github.com/braincrafted/arrayquery

ArrayQuery , API , .

$thorinsCompany = [
    [ 'name' => 'Bilbo Baggins', 'race' => 'Hobbit' ],
    [ 'name' => 'Gandalf', 'race' => 'Wizard' ],
    [ 'name' => 'Thorin Oakenshild', 'race' => 'Dwarf' ],
    [ 'name' => 'Balin', 'race' => 'Dwarf'],
    [ 'name' => 'Bifur', 'race' => 'Dwarf'],
    // ...
];

$query->from($thorinsCompany);
$query->where('race', 'Dwarf')
$query->where('age', 50, '>');
$results = $query->findAll();

, PHP- Mongo SQL Mongo API, http://www.php.net/manual/en/mongo.sqltomongo.php. , SQL PHP.

0

, , PHP SQL , - . - ArrayQuery ( ) . , .

ArrayQuery Doctrines QueryBuilder, SQL , , PHP.

0

Source: https://habr.com/ru/post/1527341/


All Articles