简体   繁体   English

多维数组树菜单子菜单功能

[英]Multidimension array tree Menu-Submenu function

i Have this PHP problem, where I should build a multidimensional tree. 我有这个PHP问题,我应该在其中建立一个多维树。

These are the strings inside the array: 这些是数组中的字符串:

 >nro_menu       > Menu ID                                                                 |
 dsc_menu       > Menu description                                                        |
 nro_menu_fk    > The parentmenu ID (menu > submenu)                                      |
 nro_ordem_menu > Show the order of childmenu inside the parentmenu                       
 leaf           > "Y" > it's a leaf, no childmenu / "N" > not a leaf, does have childmenu |

And the PHP code provided: 并且提供了PHP代码:

    $rs = array(
array(
    'nro_menu' => 3136,
    'dsc_menu' => 'Pedidos',
    'nro_menu_fk' => 1,
    'nro_ordem_menu' => '{0}',
    'leaf' => 'n'
),
array(
    'nro_menu' => 3137,
    'dsc_menu' => 'Relatórios',
    'nro_menu_fk' => 1,
    'nro_ordem_menu' => '{1}',
    'leaf' => 'n'
),
array(
    'nro_menu' => 4119,
    'dsc_menu' => 'Lançar',
    'nro_menu_fk' => 3136,
    'nro_ordem_menu' => '{0,0}',
    'leaf' => 'y'
),
array(
    'nro_menu' => 4120,
    'dsc_menu' => 'Manutenção',
    'nro_menu_fk' => 3136,
    'nro_ordem_menu' => '{0,1}',
    'leaf' => 'y'
),
array(
    'nro_menu' => 3138,
    'dsc_menu' => 'Emitir Pedido',
    'nro_menu_fk' => 3137,
    'nro_ordem_menu' => '{1,0}',
    'leaf' => 'y'
),
array(
    'nro_menu' => 3139,
    'dsc_menu' => 'Pedidos Colocados No Mês',
    'nro_menu_fk' => 3137,
    'nro_ordem_menu' => '{1,1}',
    'leaf' => 'y'
),
array(
    'nro_menu' => 3140,
    'dsc_menu' => 'Histórico Do Cliente',
    'nro_menu_fk' => 3137,
    'nro_ordem_menu' => '{1,2}',
    'leaf' => 'y'
),
array(
    'nro_menu' => 3141,
    'dsc_menu' => 'Entregas Efetuadas',
    'nro_menu_fk' => 3137,
    'nro_ordem_menu' => '{1,3}',
    'leaf' => 'y'
)
);

$tree = array();
buildTree($tree, $rs);

echo "<pre>", print_r($tree, true);

I have to build the "buildTree" function, using the $tree and $rs to create an output that show all the menus and submenus at once, no matter how deep they are. 我必须构建“ buildTree”函数,使用$ tree和$ rs创建一个输出,该输出一次显示所有菜单和子菜单,无论它们的深度如何。 Something like this: 像这样:

Array
(
[Pedidos] => Array
 (
  [Lançar] => 4119
  [Manutenção] => 4120
)
 [Relatórios] => Array
(
  [Emitir Pedido] => 3138
  [Pedidos Colocados No Mês] => 3139
  [Histórico do Cliente] => 3140
  [Entregas Efetuadas] => 3141
    )
)

I need to finish this exercise to be approved at a job. 我需要完成此练习才能获得工作批准。

But I did not find logic to construct the function. 但是我没有发现构造函数的逻辑。 Someone help me ??? 谁来帮帮我 ??? please

Try this: 尝试这个:

function buildTree($ary){
  $output = '';
  foreach($ary as $a){
    $output .= '<dl>';
    foreach($a as $i => $v){
      $output .= "<dt>$i</dt><dd>$v</dd>";
    }
    $output .= '</dl>';
  }
  return $output;
}
echo buildTree($rs);

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

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