| 1 | <pre><?php |
| 2 | $result = $this->db->safe_query_fetch('SELECT * FROM category'); |
| 3 | |
| 4 | function addleaf(&$tree, $levels, $category='') { |
| 5 | if(!count($levels)) return; |
| 6 | $current = array_shift($levels); |
| 7 | $category .= '/'.$current; |
| 8 | if(!isset($tree[$current])) $tree[$current] = array(); |
| 9 | $tree[$current]['__PATH__'] = $category; |
| 10 | addleaf($tree[$current], $levels, $category); |
| 11 | } |
| 12 | |
| 13 | $tree = array(); |
| 14 | foreach($result as $row) { |
| 15 | $row_parts = preg_split('/\//', $row['category_name']); |
| 16 | addleaf($tree, $row_parts); |
| 17 | } |
| 18 | |
| 19 | print_r($tree); |