PHP uses the idea of transferring references to implement unlimited classification (simple code) and php reference. PHP uses the idea of transferring references to implement unlimited classification (simple code). php references unlimited classification by storing the id and path of the upper-level classification to implement it in my Simpla, how does PHP implement unlimited classification (simple code) through the idea of transferring references? php references
Unlimited classification is mainly implemented by storing the id and path of the upper-level classification.
In my Simpla, I used unlimited classification. I used the reference transfer idea of PHP to implement an infinite classification method, which perfectly demonstrated a classification mode similar to this.
Id pid name
1 0 Sichuan
2 0 Chongqing
3 1 Chengdu
4 1 Mianyang
5 3 high-tech zones
The code is as follows:
/*** 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 ;}
The above content is very simple. if there is an error or a better method, we hope we can communicate with each other. Thank you .!
Callback (simple code), php references unlimited classification, mainly by storing the id of the upper-level classification and the classification path to achieve in my Simpla ,...