PHP code example of nahid / jsonq

1. Go to this page and download the library: Download nahid/jsonq 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/ */

    

nahid / jsonq example snippets


use Nahid/JsonQ/Jsonq;
$jsonq = new Jsonq('data.json');

$json->json('{"id": 1, "name": "Nahid"}');

$json->collect(['id'=>1, 'name'=>'Nahid']);

use Nahid\JsonQ\Jsonq;

$q = new Jsonq('data.json');
$res = $q->from('products')
    ->where('cat', '=', 2)
    ->get();
dump($res);

//This will print
/*
array:3 [▼
  1 => {#7 ▼
    +"id": 2
    +"user_id": 2
    +"city": null
    +"name": "macbook pro"
    +"cat": 2
    +"price": 150000
  }
  4 => {#8 ▼
    +"id": 5
    +"user_id": 1
    +"city": "bsl"
    +"name": "macbook air"
    +"cat": 2
    +"price": 110000
  }
  5 => {#9 ▼
    +"id": 6
    +"user_id": 2
    +"city": null
    +"name": "macbook air 1"
    +"cat": 2
    +"price": 81000
  }
]
*/

$result = $json->from('products')
        ->where('cat', '=', 2)
        ->sum('price');
dump($result);

//It will print:
/*
365000
*/

$q = new Jsonq('data.json');
echo $q->find('vendor.name');

$q = new Jsonq('data.json');
echo $q->find('vendor.name');

$q = new Jsonq('data.json');
echo $q->from('vendor.name')->get();

$q = new Jsonq('data.json');
echo $q->from('users.5.visits')->get();

$q = new Jsonq('data.json');
$res = $q->from('users')->where('id', '=', 1)->get();

$q = new Jsonq('data.json');
$res = $q->from('users')
->where('id', '=', 1)
->where('location', '=', 'barisal')
->get();

$q = new Jsonq('data.json');
$res = $q->from('users')
->where('id', '=', 1)
->orWhere('id', '=', 2)
->get();

$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->sum('price');

$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->count();

$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->max('price);

$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->min('price');

$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->avg('price');

$q = new jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->first();

$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->last();

$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->nth(2);

$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->exists();

$q = new Jsonq('data.json');
$res = $q->from('users')
->groupBy('location')
->get();

$q = new Jsonq();
$res = $q->collect([7, 5, 9, 1, 3])
->sort();

$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->sortBy('price', 'desc');