在树结构中显示数组

时间:2015-09-07 12:11:50

标签: php arrays multidimensional-array

  1. 我有一个数组($ select_categories)从我的类别表中加载类别

  2. 我想以下列方式显示它(如下所示)     image ...)

  3. 显示第1级,然后是第2级

  4. DB DESIGN

    Db条件

    is_parent:0,0,0

    is_child:PARENT_ID,0,0

    is_sub_child:PARENT_ID,CHILD_ID,0

       Array
        (
            [0] => Array
                (
                    [category_id] => 1
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => live
                    [is_parent] => 0
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 02:52:59
                    [updated_on] => 2015-09-07 02:22:59
                )
    
            [1] => Array
                (
                    [category_id] => 2
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => for sale
                    [is_parent] => 1
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 02:54:29
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [2] => Array
                (
                    [category_id] => 3
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => for rent
                    [is_parent] => 1
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 02:54:50
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [3] => Array
                (
                    [category_id] => 4
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => hostels
                    [is_parent] => 1
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 02:56:46
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [4] => Array
                (
                    [category_id] => 5
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => pg accomodation
                    [is_parent] => 1
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 02:57:37
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [5] => Array
                (
                    [category_id] => 6
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => villas
                    [is_parent] => 1
                    [is_child] => 2
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 02:59:16
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [6] => Array
                (
                    [category_id] => 7
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => plot
                    [is_parent] => 1
                    [is_child] => 2
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 02:59:40
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [7] => Array
                (
                    [category_id] => 8
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => Flats
                    [is_parent] => 1
                    [is_child] => 2
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:00:03
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [8] => Array
                (
                    [category_id] => 9
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => houses
                    [is_parent] => 1
                    [is_child] => 2
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:00:19
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [9] => Array
                (
                    [category_id] => 10
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => in house
                    [is_parent] => 1
                    [is_child] => 3
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:00:57
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [10] => Array
                (
                    [category_id] => 11
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => appartment
                    [is_parent] => 1
                    [is_child] => 3
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:01:19
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [11] => Array
                (
                    [category_id] => 12
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => boys
                    [is_parent] => 1
                    [is_child] => 4
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:01:44
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [12] => Array
                (
                    [category_id] => 13
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => girls
                    [is_parent] => 1
                    [is_child] => 4
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:01:58
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [13] => Array
                (
                    [category_id] => 14
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => work
                    [is_parent] => 0
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:03:48
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [14] => Array
                (
                    [category_id] => 15
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => jobs
                    [is_parent] => 14
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:04:32
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [15] => Array
                (
                    [category_id] => 16
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => government
                    [is_parent] => 14
                    [is_child] => 15
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:04:48
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [16] => Array
                (
                    [category_id] => 17
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => private
                    [is_parent] => 14
                    [is_child] => 15
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:07:09
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [17] => Array
                (
                    [category_id] => 18
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => company
                    [is_parent] => 14
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:08:30
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [18] => Array
                (
                    [category_id] => 19
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => construction
                    [is_parent] => 14
                    [is_child] => 18
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:09:34
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [19] => Array
                (
                    [category_id] => 20
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => manufacturing
                    [is_parent] => 14
                    [is_child] => 18
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:09:48
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [20] => Array
                (
                    [category_id] => 21
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => IT companies
                    [is_parent] => 14
                    [is_child] => 18
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:10:37
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [21] => Array
                (
                    [category_id] => 22
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => Enjoy
                    [is_parent] => 0
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:10:51
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [22] => Array
                (
                    [category_id] => 23
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => cinemas
                    [is_parent] => 22
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:11:15
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [23] => Array
                (
                    [category_id] => 24
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => parks
                    [is_parent] => 22
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:11:33
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [24] => Array
                (
                    [category_id] => 25
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => adventures
                    [is_parent] => 22
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:12:06
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [25] => Array
                (
                    [category_id] => 26
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => studios
                    [is_parent] => 22
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:12:20
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [26] => Array
                (
                    [category_id] => 27
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => Study
                    [is_parent] => 0
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:12:48
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [27] => Array
                (
                    [category_id] => 28
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => schools
                    [is_parent] => 27
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:13:09
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [28] => Array
                (
                    [category_id] => 29
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => colleges
                    [is_parent] => 27
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:13:24
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [29] => Array
                (
                    [category_id] => 30
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => universities
                    [is_parent] => 27
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:14:01
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [30] => Array
                (
                    [category_id] => 31
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => move
                    [is_parent] => 0
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:14:40
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [31] => Array
                (
                    [category_id] => 32
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => airline
                    [is_parent] => 31
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:15:02
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [32] => Array
                (
                    [category_id] => 33
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => railway
                    [is_parent] => 31
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:15:24
                    [updated_on] => 2015-09-07 02:47:23
                )
    
            [33] => Array
                (
                    [category_id] => 34
                    [category_slug] => 
                    [category_glyphicon] => fa fa-home fa-2x
                    [category_name] => road
                    [is_parent] => 31
                    [is_child] => 0
                    [is_sub_child] => 0
                    [rf_flag] => 0
                    [status] => 1
                    [category_description] => 
                    [created_on] => 2015-09-07 03:15:40
                    [updated_on] => 2015-09-07 02:47:23
                )
    
        )
    

    enter image description here

    我试过这样的事......

    $ select_categories是类别数组

    $ select_categories = $ this-> category1_model-> select_categories();

            $tree = array();
            foreach ($select_categories as $k => $arr) {
    
                $tree[$arr['category_id']] = $arr;
                $tree[$arr['category_id']]['is_child'] = array();
            }
    
    
            foreach ($tree as $k => &$new_a) {
                $parent = $tree[$k]['is_parent'];
                if(isset($tree[$parent])){
                    $tree[$parent]['is_child'][] = & $tree[$k];
                }
            }
    

    需要这种方式

    live
        for sale
        for rent
        hostels
        pg accomodation
    
    
    work
        jobs
        company
    
    Enjoy
        cinemas
        parks
        adventures
        studios
    Study
        schools
        colleges
        universities
    move
        airline
        railway
        road
    

1 个答案:

答案 0 :(得分:1)

也许你应该尝试一些有用但不优雅的东西来重新排序你的阵列:

$result = array();
foreach($array AS $k => $line) {
    if($line['is_child'] == 0) {
        $result[$k] => $line;
        foreach($array AS $k2 => $line2) {
            if($line2['is_child'] > 0 && $line2['is_child'] == $line['category_id']) {
                $result[$k]['children'][$k2] = $line2;
                foreach($array AS $k3 => $line3) {
                    if($line3['is_sub_child'] > 0 && $line3['is_sub_child'] == $line2['category_id']) {
                        $result[$k]['children'][$k2]['subchildren'][$k3] = $line3;
                    }
                }
            }
        }
    }
}

然后,在foreach的foreach中使用foreach;)