How can I simplify php unlimited classification?

Source: Internet
Author: User
The Array is 'array ({code...}) 'and I can only output a three-level loop to list them. {code...} is this inefficient? How to simplify writing? Or an infinite loop... Array is
'Array
(

[0] => Array ([id] => 1 [pid] => 0 [name] => Anhui [son] => Array ([0] => Array ([ id] => 3 [pid] => 1 [name] => Hefei [son] => Array ([0] => Array ([id] => 4 [pid] => 3 [name] => Changfeng County ))) [1] => Array ([id] => 5 [pid] => 1 [name] => Anqing ))) [1] => Array ([id] => 2 [pid] => 0 [name] => Zhejiang)

)'

Then I listed them as dummies, but I can only output a three-level loop.

Foreach (the above array as $ row) {echo $ row ['id']; echo $ row ['name']; if (isset ($ row ['son']) {foreach ($ row ['son'] as $ k => $ v) {echo $ v ['id']; echo $ v ['name']; if (isset ($ v ['son']) {foreach ($ v ['son'] as $ m => $ n) {echo $ n ['id']; echo $ n ['name'] ;}}}

Isn't the write efficiency good?
How to simplify writing? Or an infinite loop...

Reply content:

Array is
'Array
(

[0] => Array ([id] => 1 [pid] => 0 [name] => Anhui [son] => Array ([0] => Array ([ id] => 3 [pid] => 1 [name] => Hefei [son] => Array ([0] => Array ([id] => 4 [pid] => 3 [name] => Changfeng County ))) [1] => Array ([id] => 5 [pid] => 1 [name] => Anqing ))) [1] => Array ([id] => 2 [pid] => 0 [name] => Zhejiang)

)'

Then I listed them as dummies, but I can only output a three-level loop.

Foreach (the above array as $ row) {echo $ row ['id']; echo $ row ['name']; if (isset ($ row ['son']) {foreach ($ row ['son'] as $ k => $ v) {echo $ v ['id']; echo $ v ['name']; if (isset ($ v ['son']) {foreach ($ v ['son'] as $ m => $ n) {echo $ n ['id']; echo $ n ['name'] ;}}}

Isn't the write efficiency good?
How to simplify writing? Or an infinite loop...

Cool, search for things and learn to solve problems by yourself.
5 lines of code do not recursively solve unlimited classification: http://www.oschina.net/code/snippet_173183_11767

I am not advertising, but I think my personal summary is good:

PHP infinitus classification implementation [simple edition]

Currently, you can only output 3 levels. recursion is required for an infinitely level. I can only give you a thought. You will know the specific Baidu.

If the code is easier to read and maintain, it is recommended to use the combination mode. If it is only for efficient output, @ cevin provides enough answers.

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.