Php recursive method to implement unlimited classification instances. Array: The code is as follows $ itemsarray (array (id1, pid0, name Level 11), array (id11, pid0, namewww.111cn.net level 12), array (id2, pid1, array:
The code is as follows: |
|
$ Items = array ( Array ('id' => 1, 'pid '=> 0, 'name' => 'Level 1 11 '), Array ('id' => 11, 'pid '=> 0, 'name' => 'Www .111cn.net level 12 '), Array ('id' => 2, 'pid '=> 1, 'name' => 'Level 2 21 '), Array ('id' => 10, 'pid '=> 11, 'name' => 'Level 2 22 '), Array ('id' => 3, 'pid '=> 1, 'name' => 'second 23 '), Array ('id' => 12, 'pid '=> 11, 'name' => 'second-level 24 '), Array ('id' => 13, 'pid '=> 12, 'name' => 'third-level 31 '), Array ('id' => 9, 'pid '=> 1, 'name' => 'second level 25 '), ); |
Function:
The code is as follows: |
|
Function formatTree ($ array, $ pid = 0 ){ $ Arr = array (); $ Tem = array (); Foreach ($ array as $ v ){ If ($ v ['pid '] ==$ pid ){ $ Tem = formatTree ($ array, $ v ['id']); // Determine whether a subarray exists $ Tem & $ v ['son'] = $ tem; $ Arr [] = $ v; } } Return $ arr; } |
The array must contain the hierarchical relationship between id and pid to specify the array value.
Articles you may be interested in
- Unlimited Classification & Tree-based Forum implementation
- Php mysql enables unlimited classification tree display of classification relationships
- Unlimited articles
- Unlimited classification
- Unlimited Classification & Tree-based Forum implementation
- Php unlimited classification code
- Php unlimited classification [recursion]
- Unlimited classification [pre-sorting and traversing tree algorithms]
- PHP unlimited classification algorithm
- Ecshop product unlimited classification code
The alias code is as follows $ items = array ('id' = 1, 'pid '= 0, 'name' = 'level 1 11'), array ('id' = 11, 'pid '= 0, 'name' = 'www .111cn.net level 12'), array ('id' = 2, 'pid '= 1 ,...