douran7929
2018-10-31 06:59
浏览 74
已采纳

如何将特定结构化数组转换为“树”结构数组?

I want to create a tree array from multidimensional array. I want to create a directory structure so I need the array in Tree format. Root directory is category name which index is Zero(0). Then after all sub directories in root directory. Kindly help me to create a tree array in PHP.

my array is given.

$array = array
(
    array(array('category_id'=>1,'parent'=>0,'category'=>'General')),
    array(
        array('category_id'=>5,'parent'=>4,'category'=>'sds'),
        array('category_id'=>4,'parent'=>1,'category'=>'ssaas'),
        array('category_id'=>1,'parent'=>0,'category'=>'General'),
    ),
    array(array('category_id'=>3,'parent'=>0,'category'=>'SOP')),
    array(
        array('category_id'=>4,'parent'=>1,'category'=>'ssaas'),
        array('category_id'=>1,'parent'=>0,'category'=>'General'),
    ),
    array(array('category_id'=>2,'parent'=>0,'category'=>'Test')),
);

Result array is

$array =    array(
    array(
        'category_id'=>1,
        'parent'=>0,
        'category'=>'General',
        'child' => array(
                'category_id'=>4,
                'parent'=>1,
                'category'=>'ssaas',
                'child' => array(
                            'category_id'=>4,
                            'parent'=>1,
                            'category'=>'sds',
                                ),
    ),
    ),
     array(
            'category_id'=>2,
            'parent'=>0,
            'category'=>'Test',
         ),
     array(
            'category_id'=>3,
            'parent'=>0,
            'category'=>'SOP',
    ),
);
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongpan5289 2018-10-31 07:55
    已采纳

    You can flat -> sort by parent -> then make a tree out of your original array:

    $array = array
    (
        array(array('category_id'=>1,'parent'=>0,'category'=>'General')),
        array(
            array('category_id'=>5,'parent'=>4,'category'=>'sds'),
            array('category_id'=>4,'parent'=>1,'category'=>'ssaas'),
            array('category_id'=>1,'parent'=>0,'category'=>'General'),
        ),
        array(array('category_id'=>3,'parent'=>0,'category'=>'SOP')),
        array(
            array('category_id'=>4,'parent'=>1,'category'=>'ssaas'),
            array('category_id'=>1,'parent'=>0,'category'=>'General'),
        ),
        array(array('category_id'=>2,'parent'=>0,'category'=>'Test')),
    );
    echo '---Original Array---';
    echo "<pre>"; print_r($array); echo "</pre>";
    
    $tree = array();
    
    // flat
    foreach($array as $leaves) 
    {
        foreach($leaves as $leaf) {
            $tree[$leaf['category_id']] = $leaf;
        }
    }
    
    //sort by parent
    uasort($tree, 'parentSort');
    function parentSort($a, $b) {
        if($a['parent'] == $b['parent']) {
            return 0;
        }
        return ($a['parent'] > $b['parent']) ? -1 : 1;
    }
    echo '---Flat Array---';
    echo "<pre>"; print_r($tree); echo "</pre>";
    
    // order
    foreach($tree as $leaf)
    {
        if ($leaf['parent'] != 0) {
    
            $tree[$leaf['parent']]['child'][] = $tree[$leaf['category_id']];
            unset($tree[$leaf['category_id']]);
        }
    }
    
    //sort back by parent
    ksort($tree);
    
    echo '---Tree Array---';
    echo "<pre>"; print_r($tree); echo "</pre>";
    
    点赞 打赏 评论
  • dongtiao1817 2018-10-31 07:38
    $arr = [];
    $g_pa = ['name' => 'grandpa'];
    $dad = ['name' => 'dad'];
    $son = ['name' => 'son']
    $another_g_pa = ['name' => 'another_grandpa'];
    
    $dad['child'] = $son;
    $g_pa['child'] = $dad;
    $arr = [$g_pa, $another_g_pa];
    

    So, here are the grandpas in array, and dad's child is son, and dad is child of grandpa. And for short code use [], not array().

    点赞 打赏 评论

相关推荐 更多相似问题