menu example - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of menu example.php

  1. <?php
  2.  
  3. $menu = [
  4.     'First item. NOT drop-down' => [
  5.         'href' => '/',
  6.     ],
  7.     'Second item. Drop-down' => [
  8.         'submenu1' => [
  9.             'href' => '/submenu',
  10.         ],
  11.     ],
  12. ];
  13.  
  14. ?>
  15.  
  16. <ul>
  17. <?php foreach ($menu as $title => $item) : ?>
  18.     <?php if (in_array('href', array_keys($item))) : ?>
  19.         <li><a href="<?= $item['href'] ?>"><?= $title ?></a></li>
  20.     <?php else: ?>
  21.         <li>
  22.             <ul>
  23.                 <?php foreach ($item as $subtitle => $subitem) : ?>
  24.                     <li><a href="<?= $subitem['href'] ?>"><?= $subtitle ?></a></li>
  25.                 <?php endforeach; ?>
  26.             </ul>
  27.         </li>
  28.     <?php endif; ?>
  29. <?php endforeach; ?>
  30. </ul>
File Description
  • menu example
  • PHP Code
  • 23 Aug-2019
  • 691 Bytes
You can Share it: