I think you might be trying to do something like that:<?php
// variables de navegación
$page='#';
$trayendo["url"]='#';
$menu=[
[
'mostrar_item_del_menu'=>'Sí',
'url'=>'#',
'titulo'=>'Primer link',
'activar_submenu'=>'Sí',
'id_menu'=>'1',
],
];
$submenu=[
[
'id_menu'=>'1',
'mostrar_item_del_menu'=>'Sí',
'url'=>'#',
'titulo'=>'Primer link',
'activar_submenu'=>'Sí',
'id_submenu'=>'1',
],
];
$submenu2=[
[
'id_submenu'=>'1',
'mostrar_item_del_menu'=>'Sí',
'url'=>'#',
'titulo'=>'Primer link',
'activar_submenu'=>'Sí',
],
];
// código original
$txt=null;
foreach($menu as $navegacion){
if($navegacion["mostrar_item_del_menu"] == "Sí"){
$txt.='<li class="';
if($navegacion["url"] == $page){
$txt.='"active"';
}
$txt.='">';
$txt.='<a class="sobre" href="';
$txt.=$navegacion["url"];
$txt.='">';
$txt.=$navegacion["titulo"];
$txt.='</a>';
if($navegacion["activar_submenu"] == "Sí"){
$txt.='<ul style="';
if($trayendo["url"] == $page){
$txt.='display: block;';
}
$txt.='">';
}
foreach($submenu as $menu2){
if($navegacion["id_menu"] == $menu2["id_menu"]){
if($menu2["mostrar_item_del_menu"] == "Sí"){
$txt.='<li class="';
if($menu2["url"] == $page){
$txt.='"active"';
}
$txt.='">';
$txt.='<a class="corriendo sobre" href="';
$txt.=$menu2["url"];
$txt.='">';
$txt.=$menu2["titulo"];
$txt.='</a>';
if($menu2["activar_submenu"] == "Sí"){
$txt.='<ul>';
}
foreach($submenu2 as $menu3){
if($menu3["id_submenu"] == $menu2["id_submenu"]){
if($menu3["mostrar_item_del_menu"] == "Sí"){
$txt.='<li class="';
if($menu3["url"] == $page){
$txt.='"active";';
}
$txt.='">';
$txt.='<a class="corriendo2 sobre" href="';
$txt.=$menu3["url"];
$txt.='">';
$txt.=$menu3["titulo"];
$txt.='</a>';
$txt.='</li>';
}
if($menu2["activar_submenu"] == "Sí"){
$txt.='</ul>';
}
$txt.='</li>';
}
}
if($navegacion["activar_submenu"] == "Sí"){
$txt.='</ul>';
}
$txt.='</li>';
}
}
}
}
}
echo $txt;
Several ribs:Although it can be done in both ways, HTML is usually inserted into PHP, not PHP in HTML. As you've been told, it's easier to read and locate keys without closing and stuff.To be able to debug your code, it makes it very easy for us to work through the context: those arrays with test data to check the operation of the code.What you need, I don't finish understanding it, but I hope, with the code something clearer, you see how to do it yourself. If you need any more help, click it a bit more in the comments or modify your question by broadening the testing arrays and showing what you intend to do, what option does not work for you, etc.