// Menu Array multidimensional
$menu = array(
'index' => array(
'title' => 'Homepage',
'include' => 'home.html',
'subitems' => array()
),
'products' => array(
'title' => 'Products',
'include' => 'products.html',
'subitems' => array(
'product1' => array(
'title' => 'product1',
'include' => 'product1.html',
'subitems' => array(
'product1.2' => array(
'title' => 'product1.2',
'include' => 'product1.2.html',
'subitems' => array()
)
)
),
'product2' => array(
'title' => 'product2',
'include' => 'product2.html',
'subitems' => array()
),
'product3' => array(
'title' => 'product3',
'include' => 'product31.html',
'subitems' => array()
),
'product4' => array(
'title' => 'product4',
'include' => 'product4.html',
'subitems' => array()
)
)
)
);
// Make menu
function makeMenu($menu_array,$is_sub = false,$list=['ul','li']){
$attr = (!$is_sub) ? ' class="menu"' : ' class="submenu"';
$child = NULL;
$menu = "<{$list[0]}{$attr}>";
foreach($menu_array as $id => $items){
foreach($items as $key => $val){
if( is_array($val) ) {
if ( !empty($val) ) $child = makeMenu($val,true,$list);
} else {
$$key = $val;
}
}//foreach
$menu .= "<{$list[1]}>";
$menu .= '<a href="'.$include.'">'.$title.'</a>';
$menu .= $child; // Sub Menu
$menu .= "</{$list[1]}>";
unset($url, $text, $sub);
}//foreach
$menu .= "</{$list[0]}>";
return $menu;
}
// retrieve the desired page with the shaft
function makeSubMenu($menu, $key) {
return makeMenu(array($menu[$key]));
}
// chain Article
function chainItem(array $array, $unset = '' ){
$ds = array();
if ( is_array($array) )
foreach((array)$unset as $arr) unset($array[$arr]);
foreach($array as $key=>$value)
if (!is_array($value)) $ds[$key] = $value;
return $ds;
}
// Build walk recursive -> single array insert database MySql
function walkRecursive($array, $ordId = true, $unset=[], $children = 'children', $i = 1, $parent = 0, &$res = [] ) {
if ( !is_array($array) ) return array();
foreach(array_values($array) as $key=>$arr) {
$das = array(
'id' => $id = $ordId ? $arr['id'] : $i++,
'parent' => $parent
);
$res[] = array_merge($das,chainItem($arr,$unset));
if( isset($arr[$children]) ) {
walkRecursive($arr[$children], $ordId, $unset, $children, $i, $id, $res );
}
}
return $res;
}
echo makeMenu($menu);
// Result of the print
<ul class="menu">
<li><a href="home.html">Homepage</a></li>
<li><a href="products.html">Products</a>
<ul class="submenu">
<li><a href="product1.html">product1</a>
<ul class="submenu">
<li><a href="product1.2.html">product1.2</a></li>
</ul>
</li>
<li><a href="product2.html">product2</a>
<ul class="submenu">
<li><a href="product1.2.html">product1.2</a></li>
</ul>
</li>
<li><a href="product31.html">product3</a>
<ul class="submenu">
<li><a href="product1.2.html">product1.2</a></li>
</ul>
</li>
<li><a href="product4.html">product4</a>
<ul class="submenu">
<li><a href="product1.2.html">product1.2</a></li>
</ul>
</li>
</ul>
</li>
</ul>
// Build walk recursive -> single array insert database MySql
header("Content-type: text/plain");
print_r( walkRecursive($menu,false,['parent','id'],'subitems') );
// Print array -> Result:
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[title] => Homepage
[include] => home.html
)
[1] => Array
(
[id] => 2
[parent] => 0
[title] => Products
[include] => products.html
)
[2] => Array
(
[id] => 3
[parent] => 2
[title] => product1
[include] => product1.html
)
[3] => Array
(
[id] => 4
[parent] => 3
[title] => product1.2
[include] => product1.2.html
)
[4] => Array
(
[id] => 4
[parent] => 2
[title] => product2
[include] => product2.html
)
[5] => Array
(
[id] => 5
[parent] => 2
[title] => product3
[include] => product31.html
)
[6] => Array
(
[id] => 6
[parent] => 2
[title] => product4
[include] => product4.html
)
)