# 2.0.4.1 Study addDropdown (ok)

{% file src="https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2FxLvPtXWzU6eR7UB0zYW8%2Ftestvn-start-addDropdown.zip?alt=media&token=578524a8-c342-4b8c-a0b0-10bc47fba036" %}

C:\xampp82\htdocs\testvn\app\MenuBuilder\MenuBuilder.php

```php
<?php
namespace App\MenuBuilder;
class MenuBuilder
{
  private $menu = [];
  private $dropdownDeep = 0;
  private $dropdown = false;
  private function addRegularLink($id, $name, $slug, $icon, $iconType, $sequence = 0)
  {
    $hasIcon = ($icon === false || strlen($icon) === 0) ? false : true;
    if ($hasIcon) {
      array_push($this->menu, [
        'id' => $id,
        'target' => 'link',
        'name' => $name,
        'slug' => $slug,
        'hasIcon' => $hasIcon,
        'iconType' => $iconType,
        'sequence' => $sequence
      ]);
    } else {
      array_push($this->menu, [
        'id' => $id,
        'target' => 'link',
        'name' => $name,
        'slug' => $slug,
        'iconType' => $iconType,
        'sequence' => $sequence
      ]);
    }
  }
  public function addLink($id, $name, $slug, $icon = false, $iconType = 'coreui', $sequence = 0)
  {
    if ($this->dropdown === true) {
      $this->addDropdownLink($id, $name, $slug, $icon, $iconType, $sequence);
    } else {
      $this->addRegularLink($id, $name, $slug, $icon, $iconType, $sequence);
    }
  }
  public function addTitle($id, $name, $icon = false, $iconType = 'coreui', $sequence = 0)
  {
    $hasIcon = ($icon === false || strlen($icon) === 0) ? false : true;
    if ($hasIcon) {
      array_push($this->menu, [
        'id' => $id,
        'target' => 'title',
        'name' => $name,
        'hasIcon' => $hasIcon,
        'iconType' => $iconType,
        'sequence' => $sequence
      ]);
    } else {
      array_push($this->menu, [
        'id' => $id,
        'target' => 'title',
        'name' => $name,
        'iconType' => $iconType,
        'sequence' => $sequence
      ]);
    }
  }
  private function innerAddElementToMenuLastPosition(&$menu, $element, $offset)
  {
    // $menu = array (
    //   0 => 
    //   array (
    //     'id' => 1,
    //     'slug' => 'link',
    //     'name' => 'Dashboard',
    //     'href' => '/',
    //     'hasIcon' => true,
    //     'icon' => 'cil-speedometer',
    //     'iconType' => 'coreui',
    //     'sequence' => 1,
    //   ),
    //   1 => 
    //   array (
    //     'id' => 2,
    //     'slug' => 'dropdown',
    //     'name' => 'Settings',
    //     'hasIcon' => true,
    //     'icon' => 'cil-calculator',
    //     'iconType' => 'coreui',
    //     'elements' => 
    //     array (
    //       0 => 
    //       array (
    //         'id' => 3,
    //         'slug' => 'link',
    //         'name' => 'Notes',
    //         'href' => '/notes',
    //         'hasIcon' => false,
    //         'sequence' => 3,
    //       ),
    //       1 => 
    //       array (
    //         'id' => 4,
    //         'slug' => 'link',
    //         'name' => 'Users',
    //         'href' => '/users',
    //         'hasIcon' => false,
    //         'sequence' => 4,
    //       ),
    //       2 => 
    //       array (
    //         'id' => 5,
    //         'slug' => 'link',
    //         'name' => 'Edit menu',
    //         'href' => '/menu/menu',
    //         'hasIcon' => false,
    //         'sequence' => 5,
    //       ),
    //       3 => 
    //       array (
    //         'id' => 6,
    //         'slug' => 'link',
    //         'name' => 'Edit menu elements',
    //         'href' => '/menu/element',
    //         'hasIcon' => false,
    //         'sequence' => 6,
    //       ),
    //     ),
    //     'sequence' => 2,
    //   ),
    // );
    $z = 1;
    $result = false;
    $menu = &$menu[count($menu) - 1];
    while (is_array($menu)) {
      if ($z === $this->dropdownDeep  - $offset) {
        array_push($menu['elements'], $element);
        $result = true;
        break;
      }
      $menu = &$menu['elements'][count($menu['elements']) - 1];
      $z++;
    }
  }
  private function addElementToMenuLastPosition($element, $offset = 0)
  {
    return $this->innerAddElementToMenuLastPosition($this->menu, $element, $offset);
  }
  private function addDropdownLink($id, $name, $slug, $icon, $iconType, $sequence = 0)
  {
    $hasIcon = ($icon === false) ? false : true;
    if ($hasIcon) {
      $this->addElementToMenuLastPosition(array(
        'id' => $id,
        'target' => 'link',
        'name' => $name,
        'slug' => $slug,
        'hasIcon' => $hasIcon,
        'icon' => $icon,
        'iconType' => $iconType,
        'sequence' => $sequence,
      ));
    } else {
      $this->addElementToMenuLastPosition(array(
        'id' => $id,
        'target' => 'link',
        'name' => $name,
        'slug' => $slug,
        'hasIcon' => $hasIcon,
        'sequence' => $sequence,
      ));
    }
  }
  public function getResult()
  {
    return $this->menu;
  }
}

```

**Study 1.**

C:\xampp82\htdocs\testvn\app\MenuBuilder\MenuBuilder.php

```php
<?php
namespace App\MenuBuilder;
class MenuBuilder
{
  private $menu = array(
    'id' => 2,
    'slug' => 'dropdown',
    'name' => 'Settings',
    'hasIcon' => true,
    'icon' => 'cil-calculator',
    'iconType' => 'coreui',
    'elements' =>
    array(
      0 =>
      array(
        'id' => 3,
        'slug' => 'link',
        'name' => 'Notes',
        'href' => '/notes',
        'hasIcon' => false,
        'sequence' => 3,
      ),
      1 =>
      array(
        'id' => 4,
        'slug' => 'link',
        'name' => 'Users',
        'href' => '/users',
        'hasIcon' => false,
        'sequence' => 4,
      )
    ),
    'sequence' => 2,
  );
  private $dropdownDeep;
  private $dropdown = false;
  private function addRegularLink($id, $name, $slug, $icon, $iconType, $sequence = 0)
  {
    $hasIcon = ($icon === false || strlen($icon) === 0) ? false : true;
    if ($hasIcon) {
      array_push($this->menu, [
        'id' => $id,
        'target' => 'link',
        'name' => $name,
        'slug' => $slug,
        'hasIcon' => $hasIcon,
        'iconType' => $iconType,
        'sequence' => $sequence
      ]);
    } else {
      array_push($this->menu, [
        'id' => $id,
        'target' => 'link',
        'name' => $name,
        'slug' => $slug,
        'iconType' => $iconType,
        'sequence' => $sequence
      ]);
    }
  }
  public function addLink($id, $name, $slug, $icon = false, $iconType = 'coreui', $sequence = 0)
  {
    if ($this->dropdown === true) {
      $this->addDropdownLink($id, $name, $slug, $icon, $iconType, $sequence);
    } else {
      $this->addRegularLink($id, $name, $slug, $icon, $iconType, $sequence);
    }
  }
  public function addTitle($id, $name, $icon = false, $iconType = 'coreui', $sequence = 0)
  {
    $hasIcon = ($icon === false || strlen($icon) === 0) ? false : true;
    if ($hasIcon) {
      array_push($this->menu, [
        'id' => $id,
        'target' => 'title',
        'name' => $name,
        'hasIcon' => $hasIcon,
        'iconType' => $iconType,
        'sequence' => $sequence
      ]);
    } else {
      array_push($this->menu, [
        'id' => $id,
        'target' => 'title',
        'name' => $name,
        'iconType' => $iconType,
        'sequence' => $sequence
      ]);
    }
  }
  private function innerAddElementToMenuLastPosition(&$menu, $element, $offset)
  {
    $z = 1;
    $result = false;
    $menu = &$menu[count($menu) - 1];
    while (is_array($menu)) {
      if ($z === $this->dropdownDeep  - $offset) {
        array_push($menu['elements'], $element);
        $result = true;
        break;
      }
      $menu = &$menu['elements'][count($menu['elements']) - 1];
      $z++;
    }
  }
  private function addElementToMenuLastPosition($element, $offset = 0)
  {
    return $this->innerAddElementToMenuLastPosition($this->menu, $element, $offset);
  }
  private function addDropdownLink($id, $name, $slug, $icon, $iconType, $sequence = 0)
  {
    $hasIcon = ($icon === false) ? false : true;
    if ($hasIcon) {
      $this->addElementToMenuLastPosition(array(
        'id' => $id,
        'target' => 'link',
        'name' => $name,
        'slug' => $slug,
        'hasIcon' => $hasIcon,
        'icon' => $icon,
        'iconType' => $iconType,
        'sequence' => $sequence,
      ));
    } else {
      $this->addElementToMenuLastPosition(array(
        'id' => $id,
        'target' => 'link',
        'name' => $name,
        'slug' => $slug,
        'hasIcon' => $hasIcon,
        'sequence' => $sequence,
      ));
    }
  }
  public function getResult()
  {
    $this->addDropdownLink("1", "aaa", "", false, "coreui", 0);
    echo '<pre>';
    var_export($this->menu);
    echo '</pre>';
    return $this->menu;
  }
}

```

Kết quả:

```php
array (
  'id' => 2,
  'slug' => 'dropdown',
  'name' => 'Settings',
  'hasIcon' => true,
  'icon' => 'cil-calculator',
  'iconType' => 'coreui',
  'elements' => 
  array (
    0 => 
    array (
      'id' => 3,
      'slug' => 'link',
      'name' => 'Notes',
      'href' => '/notes',
      'hasIcon' => false,
      'sequence' => 3,
    ),
    1 => 
    array (
      'id' => 4,
      'slug' => 'link',
      'name' => 'Users',
      'href' => '/users',
      'hasIcon' => false,
      'sequence' => 4,
    ),
  ),
  'sequence' => 2,
  0 => 
  array (
    'id' => 1,
    'target' => 'link',
    'name' => 'Dashboard',
    'slug' => '/',
    'iconType' => 'coreui',
    'sequence' => 1,
  ),
  1 => 
  array (
    'id' => 13,
    'target' => 'title',
    'name' => 'Theme',
    'iconType' => 'coreui',
    'sequence' => 13,
  ),
  2 => 
  array (
    'id' => 14,
    'target' => 'link',
    'name' => 'Colors',
    'slug' => '/colors',
    'iconType' => 'coreui',
    'sequence' => 14,
  ),
  3 => 
  array (
    'id' => 15,
    'target' => 'link',
    'name' => 'Typography',
    'slug' => '/typography',
    'iconType' => 'coreui',
    'sequence' => 15,
  ),
  4 => 
  array (
    'id' => 38,
    'target' => 'link',
    'name' => 'Charts',
    'slug' => '/charts',
    'iconType' => 'coreui',
    'sequence' => 38,
  ),
  5 => 
  array (
    'id' => 47,
    'target' => 'link',
    'name' => 'Widgets',
    'slug' => '/widgets',
    'iconType' => 'coreui',
    'sequence' => 47,
  ),
  6 => 
  array (
    'id' => 48,
    'target' => 'title',
    'name' => 'Extras',
    'iconType' => 'coreui',
    'sequence' => 48,
  ),
  7 => 
  array (
    'id' => 54,
    'target' => 'link',
    'name' => 'Download CoreUI',
    'slug' => 'https://coreui.io',
    'iconType' => 'coreui',
    'sequence' => 54,
  ),
  8 => 
  array (
    'id' => 55,
    'target' => 'link',
    'name' => 'Try CoreUI PRO',
    'slug' => 'https://coreui.io/pro/',
    'iconType' => 'coreui',
    'sequence' => 55,
  ),
  16 => NULL,
)
```
