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.