Infinitus classification (1) Get the tree structure and the infinite Classification Tree Structure
Data Table Structure
Function:
Function make_tree ($ list, $ pk = 'id', $ pid = 'pid', $ child = '_ child', $ root = 0 ){
$ Tree = array ();
$ PackData = array ();
// Convert the Array Structure
Foreach ($ list as $ data ){
$ PackData [$ data [$ pk] = $ data;
}
Foreach ($ packData as $ key => $ val ){
If ($ val [$ pid] ==$ root) {// represents a node
$ Tree [] = & $ packData [$ key];
} Else {
// Find the parent class
$ PackData [$ val [$ pid] [$ child] [] = & $ packData [$ key];
}
}
Return $ tree;
}
$ Arr = make_tree ($ list, 'cat _ id', "parent_id", "son", 0 );
Var_dump ($ arr );
Result:
Array (5) {[0] => array (6) {["cat_id"] => string (1) "1" ["cat_name"] => string (6) "Women's Wear" ["parent_id"] => string (1) "0" ["sort_order"] => string (2) "50" ["is_show"] => string (1) "1" ["son"] => array (3) {[0] => array (6) {["cat_id"] => string (1) "6" ["cat_name"] => string (9) "dress" ["parent_id"] => string (1) "1" ["sort_order"] => string (2) "50" ["is_show"] => string (1) "1" ["son"] => array (1) {[0] => array (5) {["cat_id"] => string (2) "31" ["cat_name"] => string (12) "My favorites" ["parent_id"] => string (1) "6" ["sort_order"] => string (2) "50" ["is_show"] => string (1) "1 "}}}
.....