How to construct the following json php array?

Source: Internet
Author: User
How to construct the following json php array? Id: 530000, name: 'Yunnan ', city :[
{
Id: 530100, name: 'Kunming city ', district :[
{Id: 530102, name: 'wuhua region '},
{Id: 530103, name: 'pilongde '},
{Id: 530111, name: 'guandu District '},
{Id: 2312, name: 'xishan District '},
{Id: 2313, name: 'dongchuan region '},
{Id: 2314, name: 'chenggong County '},
{Id: 2315, name: 'jinning '},
{Id: 2316, name: 'fumin '},
{Id: 2317, name: 'iliang County '},
{Id: 2318, name: 'shilin Yi autonomous county '},
{Id: 2319, name: 'songming County '},
{Id: 2320, name: 'luhui Yi Miao autonomous county '},
{Id: 2321, name: 'xundian Hui Yi autonomous county '},
{Id: 2322, name: 'anning '}
]
},
{
Id: 266, name: 'qujing city ', district :[
{Id: 2323, name: 'qilin region '},
{Id: 2324, name: 'maron '},
{Id: 2325, name: 'luliang County '},
{Id: 2326, name: 'shizongxian '},
{Id: 2327, name: 'luoping County '},
{Id: 2328, name: 'fuyuan County '},
{Id: 2329, name: 'huize County '},
{Id: 2330, name: 'zhanyixian '},
{Id: 2331, name: 'xuanweishi '}
]
}
......]


The above json Array will be returned,
$ P = array (); $ city = array (); $ district = array (); $ cityList = query ("this is to query all the cities saved by a certain database "); $ districtList = query ("This is to query all zones in a city ");



How can I splice the above array and finally return a json data through json_encode ($ p);? thank you!


Reply to discussion (solution)

Who can help?

Check and associate the array.

,

Json_encode

Not to mention the difficulty of this question. this is a question, not a technical point.
When you encounter a problem, you must first check the relevant information.
Http://www.w3school.com.cn/php/php_arrays.asp
Http://www.runoob.com/php/php-json.html
After learning the basic knowledge, try to implement it by yourself.
If you encounter difficulties during implementation, you can ask questions again, so that someone will help you even if you have minor problems.

$ CityList = query ("This is to query all the cities saved by one ");
For ($ cityList) for each city
$ DistrictList = query ("This is to query all zones in a city ");
Attaches $ districtList to $ cityList

This depends on your table structure.
Then write a two-tier loop. First, the province's local pid = 0 and the rest of the underground municipal pid = the province's id

For example, Shandong province id = 1 pid = 0 Jinan City id = 2 pid = 1 Qingdao City id = 3 pid = 1

$ CityList = query ("This is to query all the cities saved by one ");
For ($ cityList) for each city
$ DistrictList = query ("This is to query all zones in a city ");
Attaches $ districtList to $ cityList




-------------------------------------
I used to do this. I don't know how to "attach $ districtList to $ cityList ",. net, directly use list. the add (otherList) add method can be used. php does not understand how to add it.

Not to mention the difficulty of this question. this is a question, not a technical point.
When you encounter a problem, you must first check the relevant information.
Http://www.w3school.com.cn/php/php_arrays.asp
Http://www.runoob.com/php/php-json.html
After learning the basic knowledge, try to implement it by yourself.
If you encounter difficulties during implementation, you can ask questions again, so that someone will help you even if you have minor problems.




========================================================== ===
This is a technical point. I just want to ask how to add a sub-array to the parent array. this knowledge point is not found in online search.

$ Parent = array ('AAA' => 1 );
$ Child = array ('CCC '=> 1 );
$ Parent ['BBB '] = $ child; // you can do this.
Print_r ($ parent );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.