Php code example for generating an unlimited topic tree

Source: Internet
Author: User
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!

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.