Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $result = $this->db->get('tbl_name')->result_array();
- $tree = getTree($result);
- if (!function_exists('getTree')) {
- function getTree($array, $id_key = 'id', $parent_id_key = 'p_id', $weight_key = 'order') {
- foreach ($array as $index => $element)
- $array[$index]['children'] = getChildren($element[$id_key], $parent_id_key, $id_key, $weight_key, $array);
- foreach ($array as $index => $element)
- if ($element[$parent_id_key] > 0)
- unset($array[$index]);
- return $array;
- }
- }
- if (!function_exists('getChildren')) {
- function getChildren($parent_id_value, $parent_id_key, $id_key, $weight_key, $array) {
- if (!is_array($array))
- return false;
- $children = array();
- foreach ($array as $key => $item) {
- if ($item[$parent_id_key] == $parent_id_value) {
- $weight = $item[$weight_key];
- while (isset($children[$weight]))
- $weight++;
- $children[$weight] = $item;
- }
- }
- foreach ($children as $key => $child) {
- $children[$key]['children'] = getChildren($child[$id_key], $parent_id_key, $id_key, $weight_key, $array);
- }
- ksort($children);
- return $children;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement