Hướng dẫn how to search data in json file using php? - Làm thế nào để tìm kiếm dữ liệu trong tệp json bằng php?

1

Show

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Đây là tệp JSON của tôi có chứa các mục của tôi. Tôi muốn tìm kiếm tên vật phẩm và trả về ID.

CODE:

 $jsonitem = file_get_contents("data.json");

 $objitems = json_decode($jsonitem);


 $findById = function($id) use ($objname) {
  foreach (json_decode($objname) as $friend) {
    if ($friend->id === $id) return $friend->name;
  }
  return;
};

echo $findById('6') ?: 'No record found.';

Tệp JSON:

[
       {
          "id":1,
          "name":"Candy Wrapper",
          "value":500,
       },
       {
          "id":2,
          "name":"Torch",
          "value":2000,
       }
    ]

Hỏi ngày 24 tháng 1 năm 2016 lúc 21:29Jan 24, 2016 at 21:29

Hướng dẫn how to search data in json file using php? - Làm thế nào để tìm kiếm dữ liệu trong tệp json bằng php?

0

Logic của bạn là chính xác, nhưng bạn có một vài lỗi trong mã của mình:

  • Bạn đang tham khảo
    $findById = function($id) use ($objitems) {
        foreach ($objitems as $friend) {
            if ($friend->id == $id) return $friend->name;
         }
    
        return false;
    };
    
    9, chưa được đặt
  • Bạn đang giải mã dữ liệu hai lần
  • Như @mikey đã chỉ ra, JSON của bạn không hợp lệ vì dấu phẩy trên các dòng
    composer require nahid/jsonq
    
    0.

Try:

$findById = function($id) use ($objitems) {
    foreach ($objitems as $friend) {
        if ($friend->id == $id) return $friend->name;
     }

    return false;
};

Đã trả lời ngày 24 tháng 1 năm 2016 lúc 21:40Jan 24, 2016 at 21:40

Mathew Tinsleymathew TinsleyMathew Tinsley

6.6152 Huy hiệu vàng28 Huy hiệu bạc37 Huy hiệu đồng2 gold badges28 silver badges37 bronze badges

3

php-jsonq

Tệp JSON là gì? Một tệp JSON lưu trữ dữ liệu trong các cặp và mảng giá trị khóa; Phần mềm nó được tạo để sau đó truy cập dữ liệu. JSON cho phép các nhà phát triển lưu trữ các loại dữ liệu khác nhau dưới dạng mã có thể đọc được của con người, với các khóa đóng vai trò là tên và các giá trị chứa dữ liệu liên quan. is a simple, elegant PHP package to Query over any type of JSON Data. It'll make your life easier by giving the flavour of an ORM-like query on your JSON.

JSONQ là một gói PHP đơn giản, thanh lịch để truy vấn bất kỳ loại dữ liệu JSON nào. Nó sẽ làm cho cuộc sống của bạn dễ dàng hơn bằng cách cung cấp hương vị của một truy vấn giống như ORM trên JSON của bạn.

Hỗ trợ cho dự án này

Này đến hạn, xin vui lòng giúp tôi cải thiện hàng ngày dự án này

Cài đặt

composer require nahid/jsonq

Cách sử dụng

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

Bạn có thể bắt đầu sử dụng gói này ngay lập tức bằng cách nhập dữ liệu JSON của mình từ một tệp:

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

Hoặc từ chuỗi JSON:

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

Hoặc từ một mảng PHP:find, where, orWhere, whereIn, whereStartsWith, whereEndsWith, whereContains and so on. Also you can aggregate your data after query using sum, count, groupBy, max, min etc.

Bạn có thể bắt đầu truy vấn dữ liệu của mình bằng các phương thức truy vấn khác nhau như Find, WHERE, WHERE, wherein, wherestartSwith, wherendswith, wherecontains, v.v. Ngoài ra, bạn có thể tổng hợp dữ liệu của mình sau khi truy vấn bằng Sum, Count, Groupby, Max, Min, v.v.

//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
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
  }
]
*/

Hãy xem một ví dụ nhanh:sum() method instead of get():

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

//It will print:
/*
365000
*/

Giả sử chúng tôi muốn có được sự tổng kết của _price_ của kết quả được truy vấn. Chúng ta có thể làm điều đó một cách dễ dàng bằng cách gọi phương thức SUM () thay vì GET ():

Khá gọn gàng, hả?

Hãy khám phá API đầy đủ để xem thư viện này có thể làm gì khác cho bạn. Chúng ta sẽ?

API

Các ví dụ API sau đây được hiển thị dựa trên dữ liệu JSON mẫu được đưa ra ở đây. Để có được một ý tưởng tốt hơn về các ví dụ, hãy xem dữ liệu JSON trước. Ngoài ra các ví dụ chi tiết về mỗi API có thể được tìm thấy ở đây.

  • Danh sách API:
  • tại
  • AVG
  • Chunk
  • sưu tầm
  • cột
  • sao chép
  • đếm
  • mỗi
  • ngoại trừ
  • tồn tại
  • tìm về
  • lọc
  • tìm thấy
  • Đầu tiên
  • từ
  • nhóm
  • ộc tự
  • nhập khẩu
  • json
  • chìa khóa
  • Cuối cùng
  • vĩ mô
  • Tối đa
  • Tối thiểu
  • thứ n
  • hoặc ở nơi nào
  • đường ống
  • cài lại
  • lựa chọn
  • kích thước
  • loại
  • Sắp xếp
  • Tổng
  • sau đó
  • tojson
  • biến đổi
  • giá trị
  • ở đâu
  • ở đâu
  • ở đó
  • trong đó
  • ở đâu
  • wherenotin
  • wherenull
  • wherenotnull

composer require nahid/jsonq 1

wherestartswith

composer require nahid/jsonq 2

  • Phương thức này sẽ thực thi các truy vấn và sẽ trả về dữ liệu kết quả. Bạn cần gọi nó cuối cùng sau khi sử dụng một số phương thức truy vấn. Chi tiết có thể được tìm thấy trong các ví dụ API khác.

`Path` - Phân cấp đường dẫn của dữ liệu bạn muốn tìm.

Bạn không cần phải gọi phương thức

composer require nahid/jsonq
1 sau này. Bởi vì phương pháp này sẽ tìm nạp và trả lại dữ liệu của chính nó. You can't chain further query methods after it. If you need that, you should use
composer require nahid/jsonq
4 or
composer require nahid/jsonq
5 method.

example:

Hãy cẩn thận: Bạn không thể chuỗi các phương thức truy vấn tiếp theo sau đó. Nếu bạn cần điều đó, bạn nên sử dụng phương thức

composer require nahid/jsonq
4 hoặc
composer require nahid/jsonq
5.

[
       {
          "id":1,
          "name":"Candy Wrapper",
          "value":500,
       },
       {
          "id":2,
          "name":"Torch",
          "value":2000,
       }
    ]
0

Giả sử bạn muốn nhận được giá trị của thuộc tính _'Cities'_ của dữ liệu JSON của bạn. Bạn có thể làm điều đó như thế này:

[
       {
          "id":1,
          "name":"Candy Wrapper",
          "value":500,
       },
       {
          "id":2,
          "name":"Torch",
          "value":2000,
       }
    ]
0

Xem một ví dụ chi tiết ở đây.

composer require nahid/jsonq 6

  • `path` (tùy chọn) - Hệ thống phân cấp đường dẫn của dữ liệu bạn muốn bắt đầu truy vấn.

Theo mặc định, truy vấn sẽ được bắt đầu từ gốc của dữ liệu JSON bạn đã đưa ra. Nếu trước tiên bạn muốn chuyển sang hệ thống phân cấp đường dẫn lồng nhau của dữ liệu từ nơi bạn muốn bắt đầu truy vấn của mình, bạn sẽ sử dụng phương thức này. Bỏ qua tham số

composer require nahid/jsonq
7 hoặc cho '.' Vì tham số cũng sẽ bắt đầu truy vấn từ dữ liệu gốc.'.' as parameter will also start query from the root Data.

Sự khác biệt giữa phương pháp này và

composer require nahid/jsonq
8 là, phương thức
composer require nahid/jsonq
8 sẽ trả về dữ liệu từ hệ thống phân cấp đường dẫn đã cho. Mặt khác, phương thức này sẽ trả về thể hiện đối tượng, để bạn có thể tiếp tục chuỗi các phương thức truy vấn sau nó.

example:

Giả sử bạn muốn bắt đầu truy vấn qua các giá trị của thuộc tính _'Vendor.name'_ của dữ liệu JSON của bạn. Bạn có thể làm điều đó như thế này:

[
       {
          "id":1,
          "name":"Candy Wrapper",
          "value":500,
       },
       {
          "id":2,
          "name":"Torch",
          "value":2000,
       }
    ]
2

Nếu bạn muốn đi qua hệ thống phân cấp sâu hơn, bạn có thể làm điều đó như:

[
       {
          "id":1,
          "name":"Candy Wrapper",
          "value":500,
       },
       {
          "id":2,
          "name":"Torch",
          "value":2000,
       }
    ]
3

Xem một ví dụ chi tiết ở đây.

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

Đây là một phương pháp bí danh của

composer require nahid/jsonq
5 và sẽ hoạt động chính xác như thế. Xem ví dụ ở đây.

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

  • `key` - tên thuộc tính của dữ liệu. Hoặc bạn có thể chuyển một chức năng ở đây để nhóm nhiều truy vấn bên trong nó. Xem chi tiết trong ví dụ
  • `val` - giá trị được khớp với. Nó có thể là một _int_, _String_, _bool_ hoặc thậm chí _function_ - tùy thuộc vào `op`.
  • `op` - toán hạng sẽ được sử dụng để kết hợp. Các toán hạng sau đây có sẵn để sử dụng:

    * `=`: Đối với khớp bình đẳng yếu * `eq`: giống như` = ` *`! Kết hợp * `seq`: giống như` == ` *`! gt`: giống như `>`

    use Nahid/JsonQ/Jsonq;
    $jsonq = new Jsonq('data.json');
    
    4: Kiểm tra xem giá trị của dữ liệu đã cho là ít hơn nữa * * `lt`: giống như` = `
    use Nahid/JsonQ/Jsonq;
    $jsonq = new Jsonq('data.json');
    
    6: Kiểm tra xem giá trị của dữ liệu đã cho là nhỏ hơn hoặc bằng với giá trị * *` lte`: Giống như `

example:

Giả sử bạn muốn tìm _'Users'_ người có _

$json->collect(['id'=>1, 'name'=>'Nahid']);
0_ của
$json->collect(['id'=>1, 'name'=>'Nahid']);
1. Bạn có thể làm điều đó như thế này:

[
       {
          "id":1,
          "name":"Candy Wrapper",
          "value":500,
       },
       {
          "id":2,
          "name":"Torch",
          "value":2000,
       }
    ]
4

Bạn có thể thêm nhiều điều kiện _where_. Nó sẽ cho kết quả bằng cách và giữa nhiều điều kiện này.

[
       {
          "id":1,
          "name":"Candy Wrapper",
          "value":500,
       },
       {
          "id":2,
          "name":"Torch",
          "value":2000,
       }
    ]
5

Xem một ví dụ chi tiết ở đây.

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

Các tham số của

$json->collect(['id'=>1, 'name'=>'Nahid']);
3 giống như
$json->collect(['id'=>1, 'name'=>'Nahid']);
4. Sự khác biệt duy nhất giữa
$json->collect(['id'=>1, 'name'=>'Nahid']);
4 và
$json->collect(['id'=>1, 'name'=>'Nahid']);
3 là: Điều kiện được đưa ra bởi phương pháp
$json->collect(['id'=>1, 'name'=>'Nahid']);
3 sẽ hoặc-ed kết quả với các điều kiện khác.

Ví dụ: nếu bạn muốn tìm người dùng có _id_ của

$json->collect(['id'=>1, 'name'=>'Nahid']);
1 hoặc
$json->collect(['id'=>1, 'name'=>'Nahid']);
9, bạn có thể làm điều đó như thế này:

[
       {
          "id":1,
          "name":"Candy Wrapper",
          "value":500,
       },
       {
          "id":2,
          "name":"Torch",
          "value":2000,
       }
    ]
6

Xem ví dụ chi tiết ở đây.

//data.json { "name": "products", "description": "Features product list", "vendor":{ "name": "Computer Source BD", "email": "", "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} ] } 0

  • `key` - tên thuộc tính của dữ liệu
  • `val` - nó phải là một mảngArray

Phương thức này sẽ hoạt động như cuộc gọi phương thức

//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
1.

//data.json { "name": "products", "description": "Features product list", "vendor":{ "name": "Computer Source BD", "email": "", "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} ] } 2

  • `key` - tên thuộc tính của dữ liệu
  • `val` - nó phải là một mảngArray

Phương thức này sẽ hoạt động như cuộc gọi phương thức

//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
1.

Phương thức này sẽ hoạt động như cuộc gọi phương thức //data.json { "name": "products", "description": "Features product list", "vendor":{ "name": "Computer Source BD", "email": "", "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} ] } 3.

  • `key` - tên thuộc tính của dữ liệu

`val` - nó phải là một mảng

//data.json { "name": "products", "description": "Features product list", "vendor":{ "name": "Computer Source BD", "email": "", "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} ] } 7

  • `key` - tên thuộc tính của dữ liệu

`val` - nó phải là một mảng

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 } ] */ 0

  • `key` - tên thuộc tính của dữ liệu
  • `val` - nó phải là một mảng

Phương thức này sẽ hoạt động như cuộc gọi phương thức

//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
1.

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 } ] */ 2

  • `key` - tên thuộc tính của dữ liệu
  • `val` - nó phải là một mảng

Phương thức này sẽ hoạt động như cuộc gọi phương thức

//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
1.

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 } ] */ 4

  • `key` - tên thuộc tính của dữ liệu
  • `val` - nó phải là một mảng

Phương thức này sẽ hoạt động như cuộc gọi phương thức

//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
1.

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 } ] */ 6

  • Phương thức này sẽ hoạt động như cuộc gọi phương thức
    //data.json
    {
    	"name": "products",
    	"description": "Features product list",
    	"vendor":{
    		"name": "Computer Source BD",
    		"email": "",
    		"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}
    	]
    }
    
    3.

example:

//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
4

[
       {
          "id":1,
          "name":"Candy Wrapper",
          "value":500,
       },
       {
          "id":2,
          "name":"Torch",
          "value":2000,
       }
    ]
7

Phương thức này sẽ hoạt động như cuộc gọi phương thức

//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
5 hoặc
//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
6.

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 } ] */ 7

Phương thức này sẽ hoạt động như cuộc gọi phương thức

//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
8 hoặc
//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
9.

example:

`val` - nó phải là một chuỗi

[
       {
          "id":1,
          "name":"Candy Wrapper",
          "value":500,
       },
       {
          "id":2,
          "name":"Torch",
          "value":2000,
       }
    ]
8

Xem ví dụ chi tiết ở đây.

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 } ] */ 8

`key` - tên thuộc tính của dữ liệu

$result = $json->from('products') ->where('cat', '=', 2) ->sum('price'); dump($result); //It will print: /* 365000 */ 0

  • Phương thức này sẽ hoạt động như cuộc gọi phương thức
    //data.json
    {
    	"name": "products",
    	"description": "Features product list",
    	"vendor":{
    		"name": "Computer Source BD",
    		"email": "",
    		"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}
    	]
    }
    
    3.

example:

//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
4

[
       {
          "id":1,
          "name":"Candy Wrapper",
          "value":500,
       },
       {
          "id":2,
          "name":"Torch",
          "value":2000,
       }
    ]
9

Phương thức này sẽ hoạt động như cuộc gọi phương thức

//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
5 hoặc
//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
6.

$result = $json->from('products') ->where('cat', '=', 2) ->sum('price'); dump($result); //It will print: /* 365000 */ 1

  • Phương thức này sẽ hoạt động như cuộc gọi phương thức
    //data.json
    {
    	"name": "products",
    	"description": "Features product list",
    	"vendor":{
    		"name": "Computer Source BD",
    		"email": "",
    		"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}
    	]
    }
    
    3.

example:

//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
4

$findById = function($id) use ($objitems) {
    foreach ($objitems as $friend) {
        if ($friend->id == $id) return $friend->name;
     }

    return false;
};
0

Phương thức này sẽ hoạt động như cuộc gọi phương thức

//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
5 hoặc
//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "",
		"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}
	]
}
6.

$result = $json->from('products') ->where('cat', '=', 2) ->sum('price'); dump($result); //It will print: /* 365000 */ 2

  • `cột` - tên thuộc tính của dữ liệu

example:

Giả sử bạn muốn tìm mức trung bình của _'Price'_ của _'Products'_. Bạn có thể làm điều đó như thế này:

$findById = function($id) use ($objitems) {
    foreach ($objitems as $friend) {
        if ($friend->id == $id) return $friend->name;
     }

    return false;
};
1

Nếu dữ liệu bạn đang truy vấn là mảng đơn giản, bạn không cần phải vượt qua tham số 'cột'. Xem ví dụ chi tiết ở đây

$result = $json->from('products') ->where('cat', '=', 2) ->sum('price'); dump($result); //It will print: /* 365000 */ 3

Nó sẽ trả về yếu tố đầu tiên của bộ sưu tập.

example:

$findById = function($id) use ($objitems) {
    foreach ($objitems as $friend) {
        if ($friend->id == $id) return $friend->name;
     }

    return false;
};
2

Xem ví dụ chi tiết ở đây.

$result = $json->from('products') ->where('cat', '=', 2) ->sum('price'); dump($result); //It will print: /* 365000 */ 4

Nó sẽ trả về yếu tố cuối cùng của bộ sưu tập.

example:

$findById = function($id) use ($objitems) {
    foreach ($objitems as $friend) {
        if ($friend->id == $id) return $friend->name;
     }

    return false;
};
3

Xem ví dụ chi tiết ở đây.

$result = $json->from('products') ->where('cat', '=', 2) ->sum('price'); dump($result); //It will print: /* 365000 */ 5

  • Nó sẽ trả về yếu tố cuối cùng của bộ sưu tập.

`index` - chỉ mục của phần tử được trả về.positive value, it will return the nth element from the beginning. If the given index is a negative value, it will return the nth element from the end.

example:

$findById = function($id) use ($objitems) {
    foreach ($objitems as $friend) {
        if ($friend->id == $id) return $friend->name;
     }

    return false;
};
4

Xem ví dụ chi tiết ở đây.

$result = $json->from('products') ->where('cat', '=', 2) ->sum('price'); dump($result); //It will print: /* 365000 */ 6

Nó sẽ trả về yếu tố cuối cùng của bộ sưu tập.true if the element is not empty or not null or not an empty array or not an empty object.

example:

`index` - chỉ mục của phần tử được trả về.

$findById = function($id) use ($objitems) {
    foreach ($objitems as $friend) {
        if ($friend->id == $id) return $friend->name;
     }

    return false;
};
5

Xem ví dụ chi tiết ở đây.

$result = $json->from('products') ->where('cat', '=', 2) ->sum('price'); dump($result); //It will print: /* 365000 */ 7

  • Nó sẽ trả về yếu tố cuối cùng của bộ sưu tập.

example:

`index` - chỉ mục của phần tử được trả về.

$findById = function($id) use ($objitems) {
    foreach ($objitems as $friend) {
        if ($friend->id == $id) return $friend->name;
     }

    return false;
};
6

Xem ví dụ chi tiết ở đây.

$result = $json->from('products') ->where('cat', '=', 2) ->sum('price'); dump($result); //It will print: /* 365000 */ 8

  • Nó sẽ trả về yếu tố cuối cùng của bộ sưu tập.ascending. You need to pass 'desc' as the _'order'_ parameter to sort the data in descending order. Also, you can pass a compare function in _'order'_ parameter to define your own logic to order the data.

`index` - chỉ mục của phần tử được trả về. This method should be used for plain Array. If you want to sort an Array of Objects you should use the sortBy() method described later.

example:

Nó sẽ trả về phần tử thứ n của bộ sưu tập. Nếu chỉ mục đã cho là một giá trị dương, nó sẽ trả về phần tử thứ n ngay từ đầu. Nếu chỉ mục đã cho là một giá trị âm, nó sẽ trả về phần tử thứ n từ cuối.

$findById = function($id) use ($objitems) {
    foreach ($objitems as $friend) {
        if ($friend->id == $id) return $friend->name;
     }

    return false;
};
7

Xem ví dụ chi tiết ở đây.

$result = $json->from('products') ->where('cat', '=', 2) ->sum('price'); dump($result); //It will print: /* 365000 */ 9

  • Nó sẽ trả về yếu tố cuối cùng của bộ sưu tập.
  • Nó sẽ trả về yếu tố cuối cùng của bộ sưu tập.ascending. You need to pass 'desc' as the _'order'_ parameter to sort the data in descending order. Also, you can pass a compare function in _'order'_ parameter to define your own logic to order the data.

`index` - chỉ mục của phần tử được trả về. This method should be used for Array of Objects. If you want to sort a plain Array you should use the sort() method described earlier.

example:

Nó sẽ trả về phần tử thứ n của bộ sưu tập. Nếu chỉ mục đã cho là một giá trị dương, nó sẽ trả về phần tử thứ n ngay từ đầu. Nếu chỉ mục đã cho là một giá trị âm, nó sẽ trả về phần tử thứ n từ cuối.

$findById = function($id) use ($objitems) {
    foreach ($objitems as $friend) {
        if ($friend->id == $id) return $friend->name;
     }

    return false;
};
8

Xem ví dụ chi tiết ở đây.

[ { "id":1, "name":"Candy Wrapper", "value":500, }, { "id":2, "name":"Torch", "value":2000, } ] 00

  • Nó sẽ trả về yếu tố cuối cùng của bộ sưu tập.

`index` - chỉ mục của phần tử được trả về.

Nó sẽ trả về phần tử thứ n của bộ sưu tập. Nếu chỉ mục đã cho là một giá trị dương, nó sẽ trả về phần tử thứ n ngay từ đầu. Nếu chỉ mục đã cho là một giá trị âm, nó sẽ trả về phần tử thứ n từ cuối.

[ { "id":1, "name":"Candy Wrapper", "value":500, }, { "id":2, "name":"Torch", "value":2000, } ] 01

Nó sẽ trả về đúng nếu phần tử không trống hoặc không null hoặc không một mảng trống hoặc không phải là một đối tượng trống.

Nó sẽ trả về phần tử thứ n của bộ sưu tập. Nếu chỉ mục đã cho là một giá trị dương, nó sẽ trả về phần tử thứ n ngay từ đầu. Nếu chỉ mục đã cho là một giá trị âm, nó sẽ trả về phần tử thứ n từ cuối.

Nó sẽ trả về đúng nếu phần tử không trống hoặc không null hoặc không một mảng trống hoặc không phải là một đối tượng trống.

Giả sử bạn muốn tìm thấy có bao nhiêu phần tử trong thuộc tính _'Products'_. Bạn có thể làm điều đó như:

`Cột` - thuộc tính mà bạn muốn nhóm bộ sưu tập.

Giả sử bạn muốn nhóm dữ liệu _'Users'_ dựa trên thuộc tính _'location'_. Bạn có thể làm điều đó như:

`order` - Nếu bạn bỏ qua thuộc tính _'order'_, dữ liệu sẽ được đặt hàng theo mặc định là tăng dần. Bạn cần truyền 'Desc' dưới dạng tham số _'order'_ để sắp xếp dữ liệu theo thứ tự giảm dần. Ngoài ra, bạn có thể chuyển một hàm so sánh trong tham số _'order'_ để xác định logic của riêng bạn để đặt hàng dữ liệu.

  • Lưu ý: Phương pháp này nên được sử dụng cho mảng trơn. Nếu bạn muốn sắp xếp một mảng các đối tượng, bạn nên sử dụng phương thức SortBy () được mô tả sau.
  • Giả sử bạn muốn sắp xếp dữ liệu _'arr'_. Bạn có thể làm điều đó như:
  • `Cột` - Bạn cần vượt qua tên cột mà việc sắp xếp sẽ được thực hiện.

Lưu ý: Phương pháp này nên được sử dụng cho mảng các đối tượng. Nếu bạn muốn sắp xếp một mảng đơn giản, bạn nên sử dụng phương thức sort () được mô tả trước đó.

Giả sử bạn muốn sắp xếp dữ liệu _'Price'_ của _'Products'_. Bạn có thể làm điều đó như:

Hướng dẫn how to search data in json file using php? - Làm thế nào để tìm kiếm dữ liệu trong tệp json bằng php?

Làm thế nào để đọc dữ liệu từ tệp JSON bằng PHP?

Sử dụng hàm file_get_contents () để đọc tệp JSON vào PHP. Hàm này được sử dụng để đọc tệp vào mã PHP ...
JSON không sử dụng thẻ cuối ..
JSON là một định dạng ngắn hơn ..
JSON nhanh hơn để đọc và viết ..
JSON có thể sử dụng mảng ..

Làm cách nào để tìm kiếm tệp JSON?

Ví dụ-1: Khóa tìm kiếm trong dữ liệu JSON đơn giản ở đây, một biến có tên CLIENTDATA được xác định để lưu trữ dữ liệu JSON.Giá trị của khóa sẽ được lấy làm đầu vào từ người dùng.Phương thức tải () của mô -đun JSON được sử dụng để tải dữ liệu JSON trong biến có tên là Khách hàng.Tiếp theo, toán tử 'In' được sử dụng để tìm kiếm khóa.'in' operator is used to search the key.

Làm thế nào có thể truy cập dữ liệu được giải mã JSON trong PHP?

PHP và JSON..
Hàm json_encode () được sử dụng để mã hóa định dạng JSON cho định dạng JSON ..
Hàm json_decode () được sử dụng để giải mã đối tượng JSON thành đối tượng PHP hoặc mảng kết hợp ..
Hàm json_decode () trả về một đối tượng theo mặc định.....
Bạn cũng có thể lặp qua các giá trị với vòng lặp foreach ():.

Cái gì chứa tệp JSON?

Tệp JSON là gì?Một tệp JSON lưu trữ dữ liệu trong các cặp và mảng giá trị khóa;Phần mềm nó được tạo để sau đó truy cập dữ liệu.JSON cho phép các nhà phát triển lưu trữ các loại dữ liệu khác nhau dưới dạng mã có thể đọc được của con người, với các khóa đóng vai trò là tên và các giá trị chứa dữ liệu liên quan.data in key-value pairs and arrays; the software it was made for then accesses the data. JSON allows developers to store various data types as human-readable code, with the keys serving as names and the values containing related data.