https://github.com/craigh411/jmespathiterator
⚡️ Access a PHP array using JMESPath
https://github.com/craigh411/jmespathiterator
arrays jmespath php
Last synced: 9 months ago
JSON representation
⚡️ Access a PHP array using JMESPath
- Host: GitHub
- URL: https://github.com/craigh411/jmespathiterator
- Owner: craigh411
- License: mit
- Created: 2021-02-11T13:08:03.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-02-11T17:15:28.000Z (over 5 years ago)
- Last Synced: 2025-10-09T16:07:58.236Z (9 months ago)
- Topics: arrays, jmespath, php
- Language: PHP
- Homepage:
- Size: 31.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JmespathIterator
`JmespathIterator` allows you to access a PHP array using [JMESPath](https://jmespath.org/) just like you would a standard array.
## Install
You can install JmespathIterator via composer:
`composer require craigh/jmespath-iterator`
## Usage
Create a new `JmespathIterator` object, then pass the JMESPath expression as the array key:
### Basic Example
```php
use Humps\Jmespath\JmespathIterator;
$iterator = new JmespathIterator([
'foo' => [
'bar' => [
'baz' => 'qux',
],
],
]);
echo $iterator['foo.bar.baz']; // output: 'qux'
```
### List Projection Example
```php
use Humps\Jmespath\JmespathIterator;
$iterator = new JmespathIterator([
'people' =>
[
[
'first' => 'James',
'last' => 'd',
],
[
'first' => 'Jacob',
'last' => 'e',
],
[
'first' => 'Jayden',
'last' => 'f',
],
[
'missing' => 'different',
],
],
'foo' =>
[
'bar' => 'baz',
],
]);
var_dump($iterator['people[*].first']); // output: ["James", "Jacob", "Jayden"]
```
### Every Level is a JmespathIterator
`JmespathIterator` always returns arrays as `JmespathIterator` objects, which means you can query nested arrays using JMESPath:
```php
use Humps\Jmespath\JmespathIterator;
$iterator = new JmespathIterator([
[
'foo' => [
'bar' => 'qux',
],
],
]);
echo $iterator[0]['foo.bar']; // output: 'qux'
```
And iterate over it just like a standard array:
```php
use Humps\Jmespath\JmespathIterator;
$iterator = new JmespathIterator([
[
'bar' => [
'baz' => 'qux',
],
],
[
'bar' => [
'baz' => 'qux',
],
],
]);
if(count($iterator)){
foreach ($iterator as $value) {
echo $value['bar.baz'];
}
}
```
### Add Items like An Array
`JmespathIterator` implements the `ArrayAccess` interface, so you can add array values just as you usually would:
```php
use Humps\Jmespath\JmespathIterator;
$iterator = new JmespathIterator();
$iterator[] = 'foo';
$iterator[] = 'bar';
echo $iterator[1] // output: 'bar';
```
### Slicing
To make things cleaner, you don't need to wrap slice expressions in square brackets, but you can if you want:
```php
use Humps\Jmespath\JmespathIterator;
$iterator = new JmespathIterator(['foo','bar','baz','qux', 'qux']);
var_dump($iterator['0::2']); // outputs: ['foo', 'baz', 'qux']
var_dump($iterator['[0::2]']); // outputs: ['foo', 'baz', 'qux']
```
### Remember: It's Not Actually An Array!
While a `JmespathIterator` object might feel like an array, it's not; but if you need it to be you can use the `toArray()` method:
```php
use Humps\Jmespath\JmespathIterator;
$iterator = new JmespathIterator(['foo','bar','baz','qux', 'quxx']);
$array = $iterator->toArray();
natsort($array);
$newIterator = new JmespathIterator($array);
var_dump($newIterator); // outputs: ['bar', 'baz','foo', 'qux', 'quxx']
```