1

I want to print this array values as my menu with this dropdown funciton. when I am printing this array with foreach it's just printing the main array offset => value. I need to print the sub array value in services as my dropdown option.

My array is :

$menuData = [
   '0' => ['Home'],
   '1' => ['About'],
   '2' => ['Contact'],
   '3' => [
      'Services' => ['Web Development', 'Wordpress', 'Mobile App'],
   ],
];

Here is my HTML navigation bar where I want to print the array: The sub-array named as services will print in the dropdown option and others will be printed in the main nav-item.

<ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <?php
                    foreach ($menuData as $value) { ?>
                        <li class="nav-item">
                            <a class="nav-link active" aria-current="page" href="#"><?php echo $value[0]; ?></a>
                        </li>
                    <?php }
                    ?>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                            Services
                        </a>
                        <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                            <li><a class="dropdown-item" href="#">Web Development</a></li>
                            <li><a class="dropdown-item" href="#">Wordpress</a></li>
                            <li><a class="dropdown-item" href="#">Mobile App</a></li>

                        </ul>
                    </li>
                </ul>

1 Answer 1

1

You can try something like this:

<?php

$menuData = [
    '0' => ['Home'],
    '1' => ['About'],
    '2' => ['Contact'],
    '3' => [
        'Services' => [
            'Web Development',
            'Wordpress',
            'Mobile App',
        ],
    ],
];

echo "<ul class=\"navbar-nav me-auto mb-2 mb-lg-0\">";

foreach ($menuData as $value) {

    $menuItems = current($value);
    $isSubArray = is_array($menuItems) || false;

    if (!$isSubArray) {
        echo "<li class=\"nav-item\">
        <a class=\"nav-link active\" aria-current=\"page\" href=\"#\">";
        echo $menuItems;
        echo "</a></li>";
    } else {
        echo "<li class=\"nav-item dropdown\">";
        echo "<a class=\"nav-link dropdown-toggle\" href=\"#\" id=\"navbarDropdown\" role=\"button\" data-bs-toggle=\"dropdown\" aria-expanded=\"false\">";
        echo key($value);
        echo "</a>";

        echo "<ul class=\"dropdown-menu\" aria-labelledby=\"navbarDropdown\">";

        foreach ($menuItems as $item) {
            echo "<li><a class=\"dropdown-item\" href=\"#\">";
            echo $item;
            echo "</a></li>";
        }

        echo "</ul>";
        echo "</li>";
    }
}

echo "</ul>";

https://3v4l.org/toFVY

Hope it helps!

Sign up to request clarification or add additional context in comments.

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.