0

I've trying to iterate the following array of PHP inside and HTML template:

$sub_menu = array(
    array(
        'titulo' => 'Ropa',
        'url' => '/ropa.html
    '),
    array(
        'titulo' =>'Electronica',
        'url' => '/electronica.html'
    ),
    array(
        'titulo' => 'Higiene',
        'url' =>'/higiene.html'
    ),
    array(
        'titulo' => 'Alimentos',
        'url' => '/alimentos.html'
    ),
    array(
        'titulo' => 'Otros',
        'url' => '/otros.html'
    )
);

Hadn't been lucky so far. What I'm trying to do is to show this as a list inside of a nav tag.

Could you please lend me a hand?

So I tried this

<?php 
echo '<ul>'; 
foreach ($sub_menu as $parent) { 
    if (is_array($parent)) { 
        echo '<ul>'; 
        foreach ($parent as $children => $key) { 
            echo '<li><a href="#">' . $children . '</a>'; 
        } 
        echo '</ul>'; 
    } 
    echo '</li>'; 
} 
echo '</ul>'; 
?>
4
  • What have you tried so far? Commented Jun 18, 2020 at 16:40
  • You speak about a HTML Template, but as a term like that can mean almost anything you may get more help if you show us what it means to you Commented Jun 18, 2020 at 16:45
  • So far I've tried this: <?php echo '<ul>'; foreach ($sub_menu as $parent) { if (is_array($parent)) { echo '<ul>'; foreach ($parent as $children => $key) { echo '<li><a href="#">' . $children . '</a>'; } echo '</ul>'; } echo '</li>'; } echo '</ul>'; ?> Commented Jun 18, 2020 at 16:48
  • Always put code in the question. Nobody can read it in a comment Commented Jun 18, 2020 at 16:53

3 Answers 3

1

I think you made it all a bit more complex than the input array required

$sub_menu = array(
    array(
        'titulo' => 'Ropa',
        'url' => '/ropa.html'),
    array(
        'titulo' =>'Electronica',
        'url' => '/electronica.html'
    ),
    array(
        'titulo' => 'Higiene',
        'url' =>'/higiene.html'
    ),
    array(
        'titulo' => 'Alimentos',
        'url' => '/alimentos.html'
    ),
    array(
        'titulo' => 'Otros',
        'url' => '/otros.html'
    )
);

echo '<ul>'.PHP_EOL; 
foreach ($sub_menu as $parent) { 
    echo '<li><a href="' . $parent['url'] . '">' . $parent['titulo'] . '</a></li>'.PHP_EOL; 
} 
echo '</ul>'.PHP_EOL; 

RESULT

<ul>
<li><a href="/ropa.html">Ropa</a></li>
<li><a href="/electronica.html">Electronica</a></li>
<li><a href="/higiene.html">Higiene</a></li>
<li><a href="/alimentos.html">Alimentos</a></li>
<li><a href="/otros.html">Otros</a></li>
</ul>
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I was looking for! Thanks!
0

Use a basic foreach() loop to render your 'navbar'... edit the HTML to suit your specific needs:

foreach ($sub_menu as $key => $value) {
        echo "<li><a href='" . $value['url'] . "'>" . $value['titulo'] . "</a></li>";
}

Comments

0

You have echo ul on wrong place try the following code

<?php   
echo "<ul>";
foreach ($sub_menu as $parent) { 
    if (is_array($parent)) { 
        echo '<li><ul>'; 
        foreach ($parent as $children => $key) { 
            echo '<li><a href="#">' . $children . '</a>'; 
        } 
        echo '</ul></li>'; 
    }else {
        echo '<li><a href="#">' . $parent . '</a>'; 
    } 

} 
 echo "</ul>";
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.