PHP Classes

PHP Array Menu List Bootstrap Navbar: Create a nested menu from an array with list items

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-05-08 (12 days ago) RSS 2.0 feedNot enough user ratingsTotal: 338 All time: 7,088 This week: 26Up
Version License PHP version Categories
array-menu 2.0.8BSD License8HTML, PHP 5, Content management
Description 

Author

This class can create a nested menu from an array with list items.

It takes as a parameter an associative array that defines a structure of menus and sub-menus.

Each menu item may have a label, a link to a given URL, and an eventual sub-menu with its items.

The class generates HTML to display the menu as a nested list of items.

Picture of William Tabata
  Performance   Level  
Name: William Tabata <contact>
Classes: 1 package by
Country: Brazil Brazil

Documentation

<h3 align="center"> ArrayMenu 2 </h3>

<p align="center"> Build Menu/Navbar from Bootstrap 5 with array[] o/ </p>

Install

composer require wtabata/arraymenu

Mode Simple

Standard HTML list structure will be built

$array = [
    'home' => [
        'name' => 'Label for Home',
        'link' => '#',
    ],
    [ /<< see, here don't have key. no matter. just for help u/
        'name' => 'Label for Contact'
    ],
];

$menu = new \Wt\ArrayMenu\Simple($array);
echo $menu;

output

<ol class="more_class my_class">
    <li class=""><a class="" href="#">Label for Home</a></li>
    <li class="">Label for Contact</li>
</ol>

Default List Tag

<ul> <!-- init - primary tag -->
    <li> <!-- item - secondary tag -->
        <a href="#"> - </a> <!-- link - content -->
    </li>
</ul>

Two ways to Configure tags

global

//set primary Tag 
$menu->setInit(['tag'=>'ol', 'class' =>'my_class']);

//set secondary Tag
$menu_b->setItem(['tag'=>'li', 'class' =>'my_class_for_item other']);

or per item

$array = [
    'config' => [ //set primary Tag 
        'tag' => 'ol',
        'class' => 'my_class'
    ],
    'home' => [
        'config' => [ //set secondary Tag
            'tag' => 'dd',
            'class' =>'my_class_for_item other'
        ],
        'name' => 'Label for Home',
        'link' => '#',
    ],
];

Mode Bootstrap 5

Bootstrap 5 navbar structure will be built

$array = [
    'home' => [
        'name' => 'Label for Home',
        'link' => '#',
        'class' => 'nav-link',
    ],
    'contact' => [
        'name' => 'Label for Contact',
        'link' => '#',
        'class' => 'nav-link',
    ],
    'sitemap' => [
        'name' => 'Label for Sitemap',
        'link' => '#',
        'class' => 'nav-link active',
    ]
];

$menu = new \WtArrayMenu\Bootstrap($array, $config_bootstrap = []);
echo $menu;

Default Config Bootstrap 5

$config_bootstrap = [
    'position' => 'default',
    'color' => 'bg-primary',
    'container' => 'container',
    'brand' => [
        'name' => 'WT.dev',
        'image' => false,
        'link' => 'https://wtabata.com'
    ],
    'off_canvas' => false
];

TO DO

Docs

  • [x] usage simple
  • [x] usage with bootstrap
  • [x] defaults tags
  • [x] custom tags
  • [ ] options base
  • [ ] options bootstrap

Base

  • [ ] other non-standard tags

Bootstrap

  • [ ] scroll

  Files folder image Files (1)  
File Role Description
Accessible without login Plain text file README.md Doc. README

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:338
This week:0
All time:7,088
This week:26Up