PHP code example of peekmo / jsonpath

1. Go to this page and download the library: Download peekmo/jsonpath library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

peekmo / jsonpath example snippets

 php
    
    
    th\JsonStore;

    $json = '...';

    $store = new JsonStore($json);

    // Returns an array with all categories from books which have an isbn attribute
    $res = $store->get("$..book[?(@.isbn)].category");
    
    $res = $store->get("$..book[?(@.isbn)].category", true); // You can set true to get only unique results

    
 php
    
    
    th\JsonStore;

    $json = '...';

    $store = new JsonStore($json);

    // Change the value of the first book's category
    $store->set("$..book[0].category", "superCategory");

    echo $store->toString();

    
 php
    
    
    th\JsonStore;

    $json = '...';

    $store = new JsonStore($json);

    // Add a new value in first book's array "key":"value"
    $store->add("$..book[0]", "value", "key");

    echo $store->toString();

    
 php
    
    
    th\JsonStore;

    $json = '...';

    $store = new JsonStore($json);

    // Removes the attribute "category" from all books
    $store->remove("$..book.*.category");

    echo $store->toString();