Nested array using PHP architecture

Source: Internet
Author: User
Use PHP to construct nested arrays. if you want to construct a nested array online, you can output JSON for foreground page cascading menus. However, this cannot be implemented at all times. The general structure is as follows: product [commod] & nbsp; |-beverage type [drink] (clsID, clsName) & nbsp; | & nbsp; & nested arrays using PHP architecture, online and so on
I want to construct a nested array to output JSON for the foreground page cascading menu, but it cannot be implemented at all times.
The general structure is as follows:
Product [commod]
|-Beverage [drink] (clsID, clsName)
|-Carbonated drinks [sodas] (itemID, itemName)
|-Fruit juice beverage [juice] (itemID, itemName)
|
|-Food [food] (clsID, clsName)
|-Fresh [fresh] (itemID, itemName)
|-Cooked (itemID, itemName)


I wrote one but it didn't work.

/**/
$ Tmparr [] = array ('clsid' => '01', 'clsname' => 'beverage class ');
Array_push ($ tmparr [0], array ('itemid' => '123', 'itemname' => 'Carbonated beverage ', 'itemrate' => '123 '));
Array_push ($ tmparr [0], array ('itemid' => '000000', 'itemname' => 'juiced beverage ', 'itemrate' => '2016 '));

$ Tmparr [] = array ('clsid '=> '02', 'clsname' => 'food food ');
$ Tmp [] = array ('itemid' => '123', 'itemname' => 'aliyun', 'itemrate' => '3% ');
$ Tmp [] = array ('itemid' => '123', 'itemname' => 'cookier', 'itemrate' => '3% ');
Array_push ($ tmparr [1], $ tmp );

$ Json_str = json_encode ($ tmparr );
// Print_r ($ tmparr );
Echo $ json_str;
?>
Architecture JSON
------ Solution --------------------
You can write it directly.
$ Tmparr ['drink '] = array ('clsid' => '01', 'clsname' => 'beverage class'); // [drink]
$ Tmparr ['drink '] ['sodas'] = array ('itemid' => '123456', 'itemname' => 'Carbonated beverage ', 'itemrate' => '123'); // [sodas]
$ Tmparr ['drink '] ['juice'] = array ('itemid' => '123456', 'itemname' => 'juice beverage ', 'itemrate' => '123'); // [juice]

$ Tmparr ['food'] = array ('clsid '=> '02', 'clsname' => 'food'); // [food]
$ Tmparr ['food'] ['fresh'] = array ('itemid' => '123', 'itemname' => 'Shanghai ', 'itemrate' => '3% '); // [fresh]
$ Tmparr ['food'] ['cooked'] = array ('itemid' => '123', 'itemname' => 'cooking ', 'itemrate' => '3% '); // [cooked]

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.