i Have this PHP problem, where I should build a multidimensional tree.
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:
$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. 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.