按父母和子女排序数组

时间:2016-05-29 17:08:46

标签: php arrays sorting multidimensional-array

我有一个这样的数组:

array (
  0 => 
  array (
    'name' => 'menus',
    'route' => 'admin.menu',
    'parent_id' => null,
  ),
  1 => 
  array (
    'name' => 'users',
    'route' => 'users',
    'parent_id' => 0,
  ),
  2 => 
  array (
    'name' => 'roles',
    'route' => 'roles',
    'parent_id' => 0,
  ),
  3 => 
  array (
    'name' => 'testmenu-3',
    'route' => 'testmenu-3',
    'parent_id' => 1,
  ),
  ...
  5 => 
  array (
    'name' => 'testmenu-5',
    'route' => 'testmenu-5',
    'parent_id' => 1,
  ),
  6 => 
  array (
    'name' => 'testmenu-6',
    'route' => 'testmenu-6',
    'parent_id' => 0,
  ),
  ...
)

我想从数组中创建一个菜单三,所以输出应该是这样的:

  menus
    users
      testmenu-3
      testmenu-5
    roles
    testmenu-6

这是构建数组的代码:

  $menu_array = [
      ['name' => 'menus', 'route' => 'admin.menu'],
      ['name' => 'users', 'route' => 'users'],
      ['name' => 'roles', 'route' => 'roles'],
  ];

  for ($i = 3; $i < 25; $i++) {
      $menu_array[$i] = [
          'name' => 'testmenu-'.$i,
          'route' => 'testmenu-'.$i,
      ];
  }

  foreach ($menu_array as $key => $menu) {
      $menu_array[$key]['parent_id'] = mt_rand(0, 5);
  }

这就是我如何对数组进行排序但由于我没有得到正确的输出而无法正常工作:

  foreach ($menu_array as $key => $item) {
      $arr[$item['parent_id']]['menus'][] = $item;
  }

但是我错过了父值,我如何在$arr[$item['parent_id']] ??

上插入它们

3 个答案:

答案 0 :(得分:1)

我无法理解数组生成部分,但是如果你有上面的数组,下面的代码就会生成菜单

filename

demo

答案 1 :(得分:0)

不确定这是不是你想要的。更像是扭曲的预感。对于实时测试,请访问:https://eval.in/578998

下面:

    <?php

            $menu_array = [
                ['name' => 'menus', 'route' => 'admin.menu'],
                ['name' => 'users', 'route' => 'users'],
                ['name' => 'roles', 'route' => 'roles'],
            ];

            for ($i = 3; $i < 25; $i++) {
                $menu_array[$i] = [
                    'name' => 'testmenu-'.$i,
                    'route' => 'testmenu-'.$i,
                ];
            }

            foreach ($menu_array as $key => $menu) {
                $menu_array[$key]['parent_id'] = mt_rand(0, 5);
            }


            $arr = array();

            foreach($menu_array as $intDex=>$arrData){
                if($arrData['name'] == "menus"){
                    if(!array_key_exists("menus", $arr)){
                        $arr["menus"]           = $arrData;
                        $menusID                = $arr['menus']['parent_id'];
                        unset($arr['menus']['parent_id']);
                        $arr['menus']['id']     = $menusID;
                    }
                }

                if($arrData['name'] == "roles"){
                    if(!array_key_exists("roles", $arr)){
                        $arr["roles"]           = $arrData;
                        $rolesID                = $arr['roles']['parent_id'];
                        unset($arr['roles']['parent_id']);
                        $arr['roles']['id']     = $rolesID;
                    }
                }

                if($arrData['name'] == "users"){
                    if(!array_key_exists("users", $arr)){
                        $arr["users"]           = $arrData;
                        $usersID                = $arr['users']['parent_id'];
                        unset($arr['users']['parent_id']);
                        $arr['users']['id']     = $usersID;
                    }
                }
            }


            foreach($menu_array as $intDex=>$arrData){
                if($arrData['name'] == "menus" || $arrData['name'] == "roles"  || $arrData['name'] == "users" ) {
                    continue;
                }else {
                    if ($arrData['parent_id'] == $arr['menus']['id']) {
                        $arr['menus'][] = $arrData;
                    }

                    if ($arrData['parent_id'] == $arr['roles']['id']) {
                        $arr['roles'][] = $arrData;
                    }

                    if ($arrData['parent_id'] == $arr['users']['id']) {
                        $arr['users'][] = $arrData;
                    }
                }
            }


            var_dump($arr);

DUMP OUTPUT:

        'menus' => 
            array (size=4)
              'name' => string 'menus' (length=5)
              'route' => string 'admin.menu' (length=10)
              'id' => int 1
              0 => 
                array (size=3)
                  'name' => string 'testmenu-5' (length=10)
                  'route' => string 'testmenu-5' (length=10)
                  'parent_id' => int 1
          'users' => 
            array (size=8)
              'name' => string 'users' (length=5)
              'route' => string 'users' (length=5)
              'id' => int 0
              0 => 
                array (size=3)
                  'name' => string 'testmenu-3' (length=10)
                  'route' => string 'testmenu-3' (length=10)
                  'parent_id' => int 0
              1 => 
                array (size=3)
                  'name' => string 'testmenu-7' (length=10)
                  'route' => string 'testmenu-7' (length=10)
                  'parent_id' => int 0
              2 => 
                array (size=3)
                  'name' => string 'testmenu-12' (length=11)
                  'route' => string 'testmenu-12' (length=11)
                  'parent_id' => int 0
              3 => 
                array (size=3)
                  'name' => string 'testmenu-14' (length=11)
                  'route' => string 'testmenu-14' (length=11)
                  'parent_id' => int 0
              4 => 
                array (size=3)
                  'name' => string 'testmenu-24' (length=11)
                  'route' => string 'testmenu-24' (length=11)
                  'parent_id' => int 0
          'roles' => 
            array (size=7)
              'name' => string 'roles' (length=5)
              'route' => string 'roles' (length=5)
              'id' => int 2
              0 => 
                array (size=3)
                  'name' => string 'testmenu-8' (length=10)
                  'route' => string 'testmenu-8' (length=10)
                  'parent_id' => int 2
              1 => 
                array (size=3)
                  'name' => string 'testmenu-16' (length=11)
                  'route' => string 'testmenu-16' (length=11)
                  'parent_id' => int 2
              2 => 
                array (size=3)
                  'name' => string 'testmenu-20' (length=11)
                  'route' => string 'testmenu-20' (length=11)
                  'parent_id' => int 2
              3 => 
                array (size=3)
                  'name' => string 'testmenu-23' (length=11)
                  'route' => string 'testmenu-23' (length=11)
                  'parent_id' => int 2

现在要获取您描述的菜单结构:

    <?php


        $output = "";

        foreach($arr as $intDex=>$arrMenu){
            $output .= "<ul>" .PHP_EOL;
            $output .= "<li><a href='{$arrMenu['route']}' data-id='{$arrMenu['id']}'>{$arrMenu['name']}</a></li>" .PHP_EOL;
            foreach($arrMenu as $iDex=>$data){
                if(is_array($data)) {
                    $output .= "<li><a href='{$data['route']}' data-pid='{$data['parent_id']}'>{$data['name']}</a></li>" .PHP_EOL;
                }
            }
            $output .= "</ul>";
        }

        echo $output;

以下是HTML Source-Code Perspective的输出结果:

        <ul>
            <li><a href='admin.menu' data-id='1'>menus</a></li>
            <li><a href='testmenu-3' data-pid='1'>testmenu-3</a></li>
            <li><a href='testmenu-6' data-pid='1'>testmenu-6</a></li>
        </ul>
        <ul>
            <li><a href='users' data-id='3'>users</a></li>
            <li><a href='testmenu-7' data-pid='3'>testmenu-7</a></li>
            <li><a href='testmenu-18' data-pid='3'>testmenu-18</a></li>
            <li><a href='testmenu-24' data-pid='3'>testmenu-24</a></li>
        </ul>
        <ul>
            <li><a href='roles' data-id='2'>roles</a></li>
            <li><a href='testmenu-4' data-pid='2'>testmenu-4</a></li>
            <li><a href='testmenu-12' data-pid='2'>testmenu-12</a></li>
            <li><a href='testmenu-17' data-pid='2'>testmenu-17</a></li>
            <li><a href='testmenu-19' data-pid='2'>testmenu-19</a></li>
            <li><a href='testmenu-20' data-pid='2'>testmenu-20</a></li>
        </ul>

        <!-- OTHERWISE-->               
        menus
            testmenu-3
            testmenu-6

        users
            testmenu-7
            testmenu-18
            testmenu-24

        roles
            testmenu-4
            testmenu-12
            testmenu-17
            testmenu-19
            testmenu-20

答案 2 :(得分:0)

假设您的阵列名称为$arrayOnline Example

$keys = array_keys($array);
array_multisort(
    array_column($array, 'parent_id'), SORT_ASC, SORT_NUMERIC, $array, $keys
);
$array = array_combine($keys, $array);

$parent = $array[0]['name'];
$out = array();

$pid = 0;
$space = '';
unset($array[0]);
foreach($array as $key => $val){
    if($pid != $val['parent_id']){
        $pid = $val['parent_id'];
        $space .= '&nbsp;&nbsp;&nbsp;&nbsp;';       
    }
    echo $space.$val['name']."<br/>";   
}