<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
$menuItems = array
(
1 => array
(
'ItemText' => 'Principal',
'ItemLink' => 'index.php',
'ParentID' => null,
),
2 => array
(
'ItemText' => 'Opciones',
'ItemLink' => 'opciones.php',
'ParentID' => null,
),
3 => array
(
'ItemText' => 'Nuevo',
'ItemLink' => 'nuevo.php',
'ParentID' => 2,
),
4 => array
(
'ItemText' => 'Empleado',
'ItemLink' => 'crearEmpleado.php',
'ParentID' => 3,
),
5 => array
(
'ItemText' => 'Proyecto',
'ItemLink' => 'crearProyecto.php',
'ParentID' => 3,
),
6 => array
(
'ItemText' => 'Obras',
'ItemLink' => 'crearObras.php',
'ParentID' => 3,
),
7 => array
(
'ItemText' => 'Contrato',
'ItemLink' => 'crearContrato.php',
'ParentID' => 3,
),
8 => array
(
'ItemText' => 'Puesto',
'ItemLink' => 'crearPuesto.php',
'ParentID' => 3,
),
9 => array
(
'ItemText' => 'Nomina',
'ItemLink' => 'crearNomina.php',
'ParentID' => 3,
),
10 => array
(
'ItemText' => 'Mostrar',
'ItemLink' => 'mostrar.php',
'ParentID' => 2,
),
11 => array
(
'ItemText' => 'Ver Todos',
'ItemLink' => 'verTodos.php',
'ParentID' => 10,
),
12 => array
(
'ItemText' => 'Editar',
'ItemLink' => 'editar.php',
'ParentID' => 2,
),
13 => array
(
'ItemText' => 'Historial',
'ItemLink' => 'historial.php',
'ParentID' => null,
),
);
// Cada nodo se inicia con 0 hijos
foreach ($menuItems as &$menuItem)
$menuItem['Children'] = array();
// Si el elemento de menú tiene ParentID,
// añadirlo a la matriz de los padres para niños
foreach ($menuItems as $ID => &$menuItem)
{
if ($menuItem['ParentID'] != null)
$menuItems[$menuItem['ParentID']]['Children'][$ID] = &$menuItem;
}
// Retirar a los niños a partir de $menuItems por lo
// que sólo los elementos de nivel superior siguen siendo
foreach (array_keys($menuItems) as $ID)
{
if ($menuItems[$ID]['ParentID'] != null)
unset($menuItems[$ID]);
}
?>
El los foreach me forman el array tal y como quiero hacer mi menu, pero no puedo representar este array en html, para que pueda crear un menu con submenus automaticamente tomando los valores de links y nombre;
ejemplo:
<ul><li><a href="index.php">Principal</a></li></ul>
