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

ActiveLibrary

rentalhost/vanilla-array-query
==============================

Extracts array elements into a new array with only queried keys.

0.1.2(4y ago)110MITPHPPHP &gt;=7.3

Since Aug 4Pushed 4y ago2 watchersCompare

[ Source](https://github.com/rentalhost/vanilla-array-query)[ Packagist](https://packagist.org/packages/rentalhost/vanilla-array-query)[ RSS](/packages/rentalhost-vanilla-array-query/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (4)Used By (0)

ArrayQuery
==========

[](#arrayquery)

A simple way to extract values from an array.

**Real world:** sometimes you need to work with an array with a lot of unnecessary information within a context, and it can be a problem. The `ArrayQuery` package can help simplify the return in a simple way, even with a multidimensional array.

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

[](#installation)

### With Composer

[](#with-composer)

```
$ composer require rentalhost/vanilla-array-query

```

```
{
    "require": {
        "rentalhost/vanilla-array-query": "^0.1"
    }
}
```

```
require 'vendor/autoload.php';

use Rentalhost\Vanilla\ArrayQuery\ArrayQuery;

ArrayQuery::query(
    [ 'name' => 'John Doe', 'age' => 30 ],
    [ 'age' ]
) === [ 'age' => 30 ];
```

Usage
-----

[](#usage)

### Simple extraction

[](#simple-extraction)

As in the example above, you can extract information easily just by specifying the keys you want to extract from the source array.

Let's assume that we have an array containing information about a page, but we want to extract only its route and title, while all other information is irrelevant at the moment.

The output will capture from the first argument (the source array) only the keys indicated by the second argument (the query array), in the order in which it is requested.

```
$page = [
    'route'       => '/home',
    'title'       => 'Home',
    'description' => 'Initial page',
];

ArrayQuery::query($page, [ 'route', 'title' ]) === [
    'route' => '/home',
    'title' => 'Home',
];
```

### Bidimensional extraction

[](#bidimensional-extraction)

It is also possible to extract information from bidimensional array in a simple way.

In this case, the query key will represent the key to be extracted from the source, and its value in array will represent the keys to be extracted from the second dimension.

So, let's assume that we have a bidimensional array containing information about a page, and we want to extract only some information related to it, keeping the original structure of the source array.

```
$page = [
    'header' => [
        'title'       => 'Home',
        'description' => 'Initial page',
    ],
];

ArrayQuery::query($page, [ 'header' => [ 'title' ] ]) === [
    'header' => [
        'title' => 'Home',
    ],
];
```

Multidimensional extraction
---------------------------

[](#multidimensional-extraction)

And going even further, we can extract information from an array even in a multidimensional source.

In this case, we pass a query containing an array of arrays, and in its keys we indicate what information we want to extract.

So, supposing that we have an array containing various information from several pages, but we are only interested in obtaining its routes, and nothing more.

```
$pages = [
    [
        'route' => '/home',
        'title' => 'Home',
    ],
    [
        'route' => '/admin',
        'title' => 'Administrative',
    ],
];

ArrayQuery::query($pages, [ [ 'route' ] ]) === [
    [
        'route' => '/home',
    ],
    [
        'route' => '/admin',
    ],
];
```

Customized extraction
---------------------

[](#customized-extraction)

In some cases, the source array is too complex in a multidimensional way, and sometimes you need to simplify the output for something easier to work with.

For example, let's say you have a page, and the only thing that interests you is the title. But the title is inside the key header with a lot of information that you don't need in context.

In this case, you can enter a custom key that has a function as a value. This function will receive all the information existing at that level, and its return will be the output to that new key.

In the example below, note that the source array does not have a title key, but we will create it on-the-fly from the existing value inside the title key that is inside the header key.

```
$page = [
    'header' => [
        'title' => 'Home',
        'description' => 'Initial page'
    ]
];

ArrayQuery::query($page, [ 'title' => static function (array $page) {
    return $page['header']['title'];
} ]) === [
    'title' => 'Home',
];
```

This functionality will also allow you to transform an existing key, filtering its elements, for example.

Let's assume, then, that we have a page with a simplified route, but we want to make it absolute. The key will be kept as a route, but its value will change.

It is important to remember that the callable will receive the entire array of that context (the `$page`, in this case), and not the value of the key you are reassigning.

```
$page = [ 'route' => '/home' ];

ArrayQuery::query($page, [ 'route' => static function (array $page) {
    return 'https://...' . $page['route'];
} ]) === [
    'route' => 'https://.../home',
];
```

### Advanced extraction

[](#advanced-extraction)

Finally, it is possible to perform an advanced extraction of values, creating keys on-the-fly through a callable as a value, but without a key.

The callable must return an array, where the keys that will be applied to the source array.

Let's assume, then, that we have several pages, and we want to use the own route as the key and the title as value.

```
$pages = [
    [
        'route' => '/home',
        'title' => 'Home'
    ],
    [
        'route' => '/admin',
        'title' => 'Administrative'
    ],
];

ArrayQuery::query($pages, [ static function (array $page) {
    return array_combine(
        array_column($page, 'route'),
        array_column($page, 'title'),
    );
} ]) === [
    '/home'  => 'Home',
    '/admin' => 'Administrative'
];
```

###  Health Score

21

—

LowBetter than 21% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~132 days

Total

3

Last Release

1792d ago

### Community

Maintainers

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

---

Top Contributors

[![rentalhost](https://avatars.githubusercontent.com/u/421851?v=4)](https://github.com/rentalhost "rentalhost (8 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

PHPackages © 2026

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