This article describes how to generate an unlimited topic tree in php. It has good reference value. Next let's take a look at the small Editor. This article mainly introduces the knowledge of generating an unlimited topic tree in php. It has good reference value. Let's take a look at it with the small editor.
Column array:
$ Arr = Array ('CID' => 2, 'cname' => 'news', 'pid' => 0), Array ('CID' => 4, 'cname' => 'sport', 'pid '=> 0), Array ('CID' => 5, 'cname' => 'enter ', 'pid' => 0), Array ('CID' => 7, 'cname' => 'hot News ', 'pid' => 2 ), array ('CID' => 8, 'cname' => 'niche News ', 'pid' => 2), Array ('CID' => 9, 'cname' => 'folk News ', 'pid' => 8 ),);
2, 'cname' => 'news', 'pid '=> 0), Array ('CID' => 4, 'cname' => 'sport ', 'pid' => 0), Array ('CID' => 5, 'cname' => 'enter', 'pid' => 0 ), array ('CID' => 7, 'cname' => 'hot News ', 'pid' => 2), Array ('CID' => 8, 'cname' => 'niche News ', 'pid' => 2), Array ('CID' => 9, 'cname' => 'folk News ', 'pid '=> 8),); function formatTree ($ array, $ pid = 0, $ level = 1, $ field = array ('pid' => 'pid ', 'id' => 'id', 'Children '=> 'Children') {$ arr = array (); foreach ($ array as & $ v) {if ($ v [$ field ['pid '] = $ pid) {$ v ['level'] = $ level; $ tem = formatTree ($ array, $ v [$ field ['id'], $ v ['level'] + 1, $ field ); // determine whether a sub-array exists. $ tem & $ v [$ field ['Children '] = $ tem; $ arr [] = $ v ;}} return $ arr;} $ tree = formatTree ($ arr, $ field = array ('pid' => 'pid', 'id' => 'CID ', 'Children '=> 'Children'); echo'';print_r( $tree); ?>
We have two methods for software design: one is to make it simple enough to make bugs invisible, and the other is to make it complex enough to make people unable to find bugs. The former is more difficult.
The above is the details shared by php code instances that generate an unlimited topic tree. For more information, see other related articles in the first PHP community!