Recently in a mall project, need to take out the data in the classification table, the classification table through a PID (parent class ID) associated with each classification, in order to organize the reading of the two-dimensional data into a tree-like format, the project has encapsulated the following method.
/** * [treecats description] * @param [Array] $arr [original unordered array] * @param [array] $limit [the first element represents Start recursive parent_id, default is 0, the second element represents the culling element and child element cat_id, if no value is passed, default is 0] * @param integer $level [depth of function call] * @return [ Array] [sorted arrays] * / Public functiontreecats($arr,$limit,$level=0) {$rec=Array();///Eliminate the value of cat_id firstforeach($arr as$key=$value){if($value[' cat_id ']!=$limit[' CD ']){$arr _new[$key]=$value; } }//traverse the new arrayforeach($arr _new as$key=$value) {if($value[' parent_id ']==$limit["PD"] &&$value[' parent_id ']!=$limit["CD"]){$value[' Level ']=$level;$rec[]=$value;//Generate an array for the next layer of loops$next _limit=Array("PD"=$value[' cat_id '],"CD"=$limit["CD"]);$rec= Array_merge ($rec,$this->treecats ($arr,$next _limit,$level+1)); } }return$rec; }
The scenario for setting up $limit here is that if you modify the parent classification of this classification to be its original sub-category, the taxonomy branch is lost, so it is time to remove its subclass from the optional class.
'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
'). Text (i)); }; $numbering. FadeIn (1700); }); });
The above describes the use of PHP real-life recursive reading products Unlimited classification, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.