简体   繁体   English

如何从另一个数组中获取树状数组?

[英]How to get a tree-like array from another array?

With a table on my database that stores items of a menu, where every item has an ID, a NAME, and a FATHER ID, I need to arrange it and get a tree-like structure of multiple levels. 我的数据库中有一个表,用于存储菜单项,其中每个项都有一个ID,一个NAME和一个FATHER ID,我需要对其进行排列并获得多层的树状结构。 What I need is an array with the top level menus, then every element with his 'childs' array that contains the sub menus, and this sub menus with their 'childs' array containing their respective sub sub menus an so for. 我需要的是一个带有顶层菜单的数组,然后是每个带有“子级”数组的元素,其中包含子菜单,而这个子菜单及其“子级”数组则包含各自的子菜单,因此。 English is no my native language so bear with me :) 英语不是我的母语,所以请忍受:)

An example for better understanding. 一个更好理解的例子。

I have the following menu in array form: 我有以下数组形式的菜单:

1- System
  2- Profile
  3- Account
    4- Info
    5- Security
6- Logout

With the following array: 使用以下数组:

$array = array(
   array('id' => 1, 'item'=>'System', 'id_father' => null),
   array('id' => 2, 'item'=>'Profile', 'id_father' => 1),
   array('id' => 3, 'item'=>'Account', 'id_father' => 2),
   array('id' => 4, 'item'=>'Info', 'id_father' => 3),
   array('id' => 5, 'item'=>'Security', 'id_father' => 3),
   array('id' => 6, 'item'=>'Logout', 'id_father' => 1)
);

How can I get the following ? 我如何获得以下内容? :

array(
  array('id' => 1, 'item'=>'System', 'id_father' => null,
     'childs' => array(
         array('id' => 2, 'item'=>'Profile', 'id_father' => 1),
         array('id' => 3, 'item'=>'Account', 'id_father' => 2,
           'childs' => array(
              array('id' => 4, 'item'=>'Info', 'id_father' => 3),
              array('id' => 5, 'item'=>'Security', 'id_father' => 3)
           ),
         ),
      ),
  ),
  array('id' => 6, 'item'=>'Logout', 'id_father' => 1)
);

Change $array to : $array更改为:

$array = array(
 array('id' => 1, 'item'=>'System', 'id_father' => null),
 array('id' => 2, 'item'=>'Profile', 'id_father' => 1),
 array('id' => 3, 'item'=>'Account', 'id_father' => 1), // set id_father = 1
 array('id' => 4, 'item'=>'Info', 'id_father' => 3),
 array('id' => 5, 'item'=>'Security', 'id_father' => 3),
 array('id' => 6, 'item'=>'Logout', 'id_father' => null) // edited to set id_father = null
);

Do it: 做到:

function tree( $ar, $pid = null ) {
$op = array();
foreach( $ar as $item ) {
    if( $item['id_father'] == $pid ) {
        $op[$item['id']] = array(
            'item' => $item['item'],
            'id_father' => $item['id_father'],
            'id' => $item['id']
        );
        // using recursion
        $children =  tree( $ar, $item['id'] );
        if( $children ) {
            $op[$item['id']]['childs'] = $children;
        }
    }
 }
 return $op;
}


$tree = tree($array);

echo '<pre>';
print_r( $tree);
echo '</pre>';

// OUTPUT

Array
(
 [1] => Array
    (
        [item] => System
        [id_father] => 
        [id] => 1
        [childs] => Array
            (
                [2] => Array
                    (
                        [item] => Profile
                        [id_father] => 1
                        [id] => 2
                    )

                [3] => Array
                    (
                        [item] => Account
                        [id_father] => 1
                        [id] => 3
                        [childs] => Array
                            (
                                [4] => Array
                                    (
                                        [item] => Info
                                        [id_father] => 3
                                        [id] => 4
                                    )

                                [5] => Array
                                    (
                                        [item] => Security
                                        [id_father] => 3
                                        [id] => 5
                                    )

                            )

                    )

            )

    )

[6] => Array
    (
        [item] => Logout
        [id_father] => 
        [id] => 6
    )

)

There is no need to recursion in this way : 无需以这种方式递归:

$pool = array();
foreach ($array as $value) {
    $pool[$value['id']] = $value;
    $pool[$value['id']]['children'] = array();
}
foreach ($pool as $k => $v) {
    if ($v['id_father']) {
        $pool[$v['id_father']]['children'][] = &$pool[$k];
    }
    else
        $parent[] = $v['id'];
}
$result = array();
foreach ($parent as $val) {
    $result = $result + $pool[$val];
}
print_r($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM