PHP ile Json Sorgulama

Json'un yapısının ne kadar geniş ve hızla geliştiğini bilmeyen yok. Ben bir projemde sisteme hız kazandırmak için php json query (sorgulama) araştırması yaptım ve çok güzel bir yapı buldum. Nahid Bin Azhar abimizin yaptığı jsonq kütüphanesi. Githubdan rahatça bulabilirsiniz. Benim yaptığım şey şu mysqlden bugünkü satışları alıp içerisinde sorgulama yapmak. A kategorisinden kaç tane B kategorisinden kaçta gibi ama sorgu çeşidi çok olduğu için mysql'i yoruyordu. Verileri jsonda toplayıp çoklu sorguları onun üzerinden yapınca sistem daha hızlı çalışıyor.Benim işime yaradı sizede tavsiye ederim.

Önce kurulum; 
composer require nahid/jsonq

//data.json
{
 "name": "products",
 "description": "Features product list",
 "vendor":{
  "name": "Computer Source BD",
  "email": "info@example.com",
  "website":"www.example.com"
 },
 "users":[
  {"id":1, "name":"Johura Akter Sumi", "location": "Barisal"},
  {"id":2, "name":"Mehedi Hasan Nahid", "location": "Barisal"},
  {"id":3, "name":"Ariful Islam", "location": "Barisal"},
  {"id":4, "name":"Suhel Ahmed", "location": "Sylhet"},
  {"id":5, "name":"Firoz Serniabat", "location": "Gournodi"},
  {"id":6, "name":"Musa Jewel", "location": "Barisal", "visits": [
   {"name": "Sylhet", "year": 2011},
   {"name": "Cox's Bazar", "year": 2012},
   {"name": "Bandarbar", "year": 2014}
  ]}
 ],
 "products": [
  {"id":1, "user_id": 2, "city": "bsl", "name":"iPhone", "cat":1, "price": 80000},
  {"id":2, "user_id": 2, "city": null, "name":"macbook pro", "cat": 2, "price": 150000},
  {"id":3, "user_id": 2, "city": "dhk", "name":"Redmi 3S Prime", "cat": 1, "price": 12000},
  {"id":4, "user_id": 1, "city": null, "name":"Redmi 4X", "cat":1, "price": 15000},
  {"id":5, "user_id": 1, "city": "bsl", "name":"macbook air", "cat": 2, "price": 110000},
  {"id":6, "user_id": 2, "city": null, "name":"macbook air 1", "cat": 2, "price": 81000}
 ]
}

Kodlama 
include 'vendor/autoload.php';
use Nahid\JsonQ\Jsonq;

$q = new Jsonq('data.json');
$res = $q->from('products')
    ->where('cat', '=', 2)
    ->get();
print_r($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
  }
]
*/

Kullanılabilen komutlar


Kaynak : https://github.com/nahid/jsonq

Yorumlar

Bu blogdaki popüler yayınlar

Stormfall: Saga of Survival Hile

Php ile Windows Uygulama Yapmak Başlangıç (PHP Desktop)

Rise Of Civilizations