Giter Club home page Giter Club logo

jmespathiterator's Introduction

JmespathIterator

JmespathIterator allows you to access a PHP array using JMESPath 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

use Humps\Jmespath\JmespathIterator;

$iterator = new JmespathIterator([
  'foo' => [
    'bar' => [
      'baz' => 'qux',
    ],
  ],
]);

echo $iterator['foo.bar.baz']; // output: 'qux'

List Projection Example

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:

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:

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:

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:

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:

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']

jmespathiterator's People

Contributors

craiggoescoding avatar craigh411 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.