PHP数组包含Items,每个项都是键和值对的数组

时间:2017-03-25 18:53:45

标签: php arrays multidimensional-array phpmyadmin

我是PHP的重要人物。我有一个从数据库返回的数组;它是关于项目的数据集合;每个项目都有键和值对形式的数据 这就是我的数组看起来的样子

[{"item_id":"1","item_title":"Grand Nile Tower","item_title_ar":"\u062c\u0631\u0627\u0646\u062f \u062d\u064a\u0627\u0629 \u0627\u0644\u0642\u0627\u0647\u0631\u0629","item_desc":"marvelous Hotel","item_desc_ar":"\u0641\u0646\u062f\u0642 \u0631\u0627\u0626\u0639","item_department_id":"1","item_views":"1","item_latitude":"25.248436","item_longitude":"45.252761","item_phone":"01069924166","item_site":"http:\/\/facebook.com","item_address":"This family-friendly hotel is located in the city center, within 1 mi (2 km) of Ethnological Museum and Egyptian Museum. ","item_address_ar":"\u064a\u0642\u0639 \u0647\u0630\u0627 \u0627\u0644\u0641\u0646\u062f\u0642 \u0627\u0644\u0645\u0646\u0627\u0633\u0628 \u0644\u0644\u0639\u0627\u0626\u0644\u0627\u062a \u0641\u064a \u0648\u0633\u0637 \u0627\u0644\u0645\u062f\u064a\u0646\u0629\u060c \u0639\u0644\u0649 \u0628\u0639\u062f 1 \u0645\u064a\u0644 (2 \u0643\u0645) \u0645\u0646 \u0645\u062a\u062d\u0641 \u0627\u0644\u0623\u0639\u0631\u0627\u0642 \u0627\u0644\u0628\u0634\u0631\u064a\u0629 \u0648\u0627\u0644\u0645\u062a\u062d\u0641 \u0627\u0644\u0645\u0635\u0631\u064a.","item_email":"m.ezzat2020@yahoo.com","item_image":"http:\/\/thabehaty.com\/ezzat\/portsaidy\/Shakraa\/images\/grand_otel.jpg","item_review":"3.5","id":"1","username":"m","comment":"mm","date":"20\/03\/2017","rate":"5"},{"item_id":"2","item_title":"Intercontinental Citystars","item_title_ar":"\u0627\u0646\u062a\u0631\u0643\u0648\u0646\u062a\u064a\u0646\u0646\u062a\u0627\u0644 \u0633\u064a\u062a\u064a \u0633\u062a\u0627\u0631\u0632 ","item_desc":"nice one","item_desc_ar":"\u0644\u0637\u064a\u0641","item_department_id":"1","item_views":"0","item_latitude":"31.248544","item_longitude":"32.279828","item_phone":"","item_site":"","item_address":"Omar Ibn El Khattab, Heliopolis, Nasr City, 11757 Cairo, Egypt","item_address_ar":"\u0639\u0645\u0631 \u0628\u0646 \u0627\u0644\u062e\u0637\u0627\u0628 , \u0647\u064a\u0644\u0648\u0628\u0644\u064a\u0633 , \u0645\u062f\u064a\u0646\u0629 \u0646\u0635\u0631 , 11757 , \u0627\u0644\u0642\u0627\u0647\u0631\u0647 \u0645\u0635\u0631","item_email":"","item_image":"http:\/\/thabehaty.com\/ezzat\/portsaidy\/Shakraa\/images\/intercontinental.jpg","item_review":"4","id":"11","username":"bnm","comment":"gogogo","date":"23\/3\/2017","rate":"5"}{"item_id":null,"item_title":"Pyramisa Downtown Residence","item_title_ar":"\u0628\u064a\u0631\u0627\u0645\u064a\u0632\u0627 \u0648\u0633\u0637 \u0627\u0644\u0625\u0642\u0627\u0645\u0629","item_desc":"","item_desc_ar":"","item_department_id":"1","item_views":"0","item_latitude":"31.258426","item_longitude":"32.293099","item_phone":"","item_site":"","item_address":"Abi Emama Street, Dokki , Dokki, Cairo, Egypt","item_address_ar":"\u0623\u0628\u064a \u0625\u0645\u0627\u0645 \u0634\u0627\u0631\u0639 \u0627\u0644\u062f\u0642\u064a\u060c \u0627\u0644\u062f\u0642\u064a\u060c \u0627\u0644\u0642\u0627\u0647\u0631\u0629\u060c \u0645\u0635","item_email":"","item_image":"http:\/\/thabehaty.com\/ezzat\/portsaidy\/Shakraa\/images\/parmisa.jpg","item_review":"0","id":null,"username":null,"comment":null,"date":null,"rate":null}]

所以我需要你的帮助来帮助我理解如何在这个数组中过滤或搜索

1 个答案:

答案 0 :(得分:0)

您需要使用json_decode转换此内容。

以下是一些演示代码以及如何循环结果。

另外,请注意,您在此问题中提供的JSON无效。您可以使用http://jsonlint.com/进行检查。酒店之间缺少逗号(就在 Pyramisa Downtown Residence 之前)。我将逗号添加到我的示例中使用的JSON中。

<?php

$str = '[{"item_id":"1","item_title":"Grand Nile Tower","item_title_ar":"\u062c\u0631\u0627\u0646\u062f \u062d\u064a\u0627\u0629 \u0627\u0644\u0642\u0627\u0647\u0631\u0629","item_desc":"marvelous Hotel","item_desc_ar":"\u0641\u0646\u062f\u0642 \u0631\u0627\u0626\u0639","item_department_id":"1","item_views":"1","item_latitude":"25.248436","item_longitude":"45.252761","item_phone":"01069924166","item_site":"http:\/\/facebook.com","item_address":"This family-friendly hotel is located in the city center, within 1 mi (2 km) of Ethnological Museum and Egyptian Museum. ","item_address_ar":"\u064a\u0642\u0639 \u0647\u0630\u0627 \u0627\u0644\u0641\u0646\u062f\u0642 \u0627\u0644\u0645\u0646\u0627\u0633\u0628 \u0644\u0644\u0639\u0627\u0626\u0644\u0627\u062a \u0641\u064a \u0648\u0633\u0637 \u0627\u0644\u0645\u062f\u064a\u0646\u0629\u060c \u0639\u0644\u0649 \u0628\u0639\u062f 1 \u0645\u064a\u0644 (2 \u0643\u0645) \u0645\u0646 \u0645\u062a\u062d\u0641 \u0627\u0644\u0623\u0639\u0631\u0627\u0642 \u0627\u0644\u0628\u0634\u0631\u064a\u0629 \u0648\u0627\u0644\u0645\u062a\u062d\u0641 \u0627\u0644\u0645\u0635\u0631\u064a.","item_email":"m.ezzat2020@yahoo.com","item_image":"http:\/\/thabehaty.com\/ezzat\/portsaidy\/Shakraa\/images\/grand_otel.jpg","item_review":"3.5","id":"1","username":"m","comment":"mm","date":"20\/03\/2017","rate":"5"},{"item_id":"2","item_title":"Intercontinental Citystars","item_title_ar":"\u0627\u0646\u062a\u0631\u0643\u0648\u0646\u062a\u064a\u0646\u0646\u062a\u0627\u0644 \u0633\u064a\u062a\u064a \u0633\u062a\u0627\u0631\u0632 ","item_desc":"nice one","item_desc_ar":"\u0644\u0637\u064a\u0641","item_department_id":"1","item_views":"0","item_latitude":"31.248544","item_longitude":"32.279828","item_phone":"","item_site":"","item_address":"Omar Ibn El Khattab, Heliopolis, Nasr City, 11757 Cairo, Egypt","item_address_ar":"\u0639\u0645\u0631 \u0628\u0646 \u0627\u0644\u062e\u0637\u0627\u0628 , \u0647\u064a\u0644\u0648\u0628\u0644\u064a\u0633 , \u0645\u062f\u064a\u0646\u0629 \u0646\u0635\u0631 , 11757 , \u0627\u0644\u0642\u0627\u0647\u0631\u0647 \u0645\u0635\u0631","item_email":"","item_image":"http:\/\/thabehaty.com\/ezzat\/portsaidy\/Shakraa\/images\/intercontinental.jpg","item_review":"4","id":"11","username":"bnm","comment":"gogogo","date":"23\/3\/2017","rate":"5"},{"item_id":null,"item_title":"Pyramisa Downtown Residence","item_title_ar":"\u0628\u064a\u0631\u0627\u0645\u064a\u0632\u0627 \u0648\u0633\u0637 \u0627\u0644\u0625\u0642\u0627\u0645\u0629","item_desc":"","item_desc_ar":"","item_department_id":"1","item_views":"0","item_latitude":"31.258426","item_longitude":"32.293099","item_phone":"","item_site":"","item_address":"Abi Emama Street, Dokki , Dokki, Cairo, Egypt","item_address_ar":"\u0623\u0628\u064a \u0625\u0645\u0627\u0645 \u0634\u0627\u0631\u0639 \u0627\u0644\u062f\u0642\u064a\u060c \u0627\u0644\u062f\u0642\u064a\u060c \u0627\u0644\u0642\u0627\u0647\u0631\u0629\u060c \u0645\u0635","item_email":"","item_image":"http:\/\/thabehaty.com\/ezzat\/portsaidy\/Shakraa\/images\/parmisa.jpg","item_review":"0","id":null,"username":null,"comment":null,"date":null,"rate":null}]';

$json = json_decode($str);

print_r($json);

foreach($json as $hotel) {
    echo $hotel->item_title;
    echo "\n";
}