PHP uses the idea of transferring references to implement unlimited classification.

Source: Internet
Author: User
PHP uses the idea of transferring references to implement unlimited classification in Simpla, and uses the idea of transferring references to PHP to implement unlimited classification, this classification mode can be perfectly displayed.

The specific implementation code is as follows: if there is an error or a better method, you may want to communicate with each other.

Id pid name1 0 Sichuan 2 0 Chongqing 3 1 Chengdu 4 1 Mianyang 5 3 hi-tech zone
/*** Convert an array into an infinite classification-transmit the reference idea * @ param array $ items * @ return array */public static function get_tree ($ orig) {// solve the problem that subscript is not from 1 $ items = array (); foreach ($ orig as $ key => $ value) {$ items [$ value ['id'] = $ value;} // Start Assembly $ tree = array (); foreach ($ items as $ key => $ item) {if ($ item ['pid '] = 0) {// if the value is 0, $ tree [] = & $ items [$ key];} else {if (isset ($ items [$ item ['pid ']) {// if a value exists, it is a second-level classification. $ items [$ item ['pid '] ['child'] [] = & $ items [$ key]; // direct value assignment and change for transferring references} else {// at least three levels of classification // because it is a reference transfer idea, $ tree [] = & $ items [$ key] ;}} return $ tree ;}

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.