: This article describes how to use recursive reading of unlimited product categories in php practice. For more information about PHP tutorials, see. Recently, when I was working on a mall project, I needed to retrieve the data in the category table. in the category table, I used a pid (parent class id) to associate each category, to organize and read the two-dimensional data into a tree format, we encapsulate the following method in the project.
/*** [TreeCats description] * @ param [array] $ arr [original unordered array] * @ param [array] $ limit [the first element indicates the start of recursive parent_id, the default value is 0. The second element indicates removing the element and the child element cat_id. If no value is passed, the default value is 0] * @ param integer $ level [function call depth] * @ return [array] [sorted array] */publicfunctiontreeCats ($ arr, $ limit, $ level = 0) {$ rec = array (); // first remove the value foreach ($ arras $ key => $ value) corresponding to the cat_id) {if ($ value ['cat _ id']! = $ Limit ['CD']) {$ arr_new [$ key] = $ value ;}/// traverse the new array foreach ($ arr_newas $ key => $ value) {if ($ value ['parent _ id'] = $ limit ["pd"] & $ value ['parent _ id']! = $ Limit ["cd"]) {$ value ['level'] = $ level; $ rec [] = $ value; // generate an array for the next loop $ next_limit = array ("pd" => $ value ['cat _ id'], "cd" => $ limit ["cd"]); $ rec = array_merge ($ rec, $ this-> treeCats ($ arr, $ next_limit, $ level + 1);} return $ rec ;}
The application scenario of setting $ limit here is that if the parent category of this category is changed to its original subcategory, the category branch will be lost, at this time, we should remove its subclass from the optional classes.
'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
'). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script
The above introduces the use of recursive reading of unlimited product categories in php practice, including some content, and hope to be helpful to friends who are interested in PHP tutorials.