0

In my previous question I wrongly considered only one [sub] key, but the whole sidebar menu is this:

$sidebar = array(
    'dashboard' => array(
        'levels' => array('1', '2'),
        'title' => 'Bacheca',
        'icon' => 'fa-dashboard',
        'url' => '/ctrl/dashboard',
    ),
    'settings' => array(
        'levels' => array('1'),
        'title' => 'Impostazioni',
        'icon' => 'fa-gear',
        'sub' => array(
            'configuration' => array(
                'levels' => array('1'),
                'title' => 'Configurazione',
                'url' => '/ctrl/configuration',
            ),
            'languages' => array(
                'levels' => array('1'),
                'title' => 'Lingue',
                'url' => '/ctrl/languages',
            ),
            'modules' => array(
                'levels' => array('1'),
                'title' => 'Moduli',
                'url' => '/ctrl/modules',
            ),
            'categories' => array(
                'levels' => array('1'),
                'title' => 'Categorie', 
                'url' => '/ctrl/categories',
            ),
        ),
    ),
    'modules' => array(
        'levels' => array('1'),
        'title' => 'Moduli',
        'icon' => 'fa-puzzle-piece',
        'sub' => array(
            'pages' => array(
                'levels' => array('1'),
                'title' => 'Pagine',
                'url' => '/ctrl/pages',
            ),
            'news' => array(
                'levels' => array('1'),
                'title' => 'News',
                'url' => '/ctrl/news',
            ),
            'contacts' => array(
                'levels' => array('1'),
                'title' => 'Contatti',
                'url' => '/ctrl/contacts',
            ),
            'location' => array(
                'levels' => array('1'),
                'title' => 'Dove siamo',
                'url' => '/ctrl/location',
            ),
            'catalog' => array(
                'levels' => array('1'),
                'title' => 'Catalogo', 
                'url' => '/ctrl/catalog',
            ),
            'gallery' => array(
                'levels' => array('1'),
                'title' => 'Gallery',
                'url' => '/ctrl/gallery',
            ),
            'slide' => array(
                'levels' => array('1'),
                'title' => 'Slide',
                'url' => '/ctrl/slide',
            ),
        ),
    ),
    'shop' => array(
        'levels' => array('1'),
        'title' => 'Shop',
        'icon' => 'fa-shopping-cart',
        'sub' => array(
            'shop-articles' => array(
                'levels' => array('1'),
                'title' => 'Articoli',
                'url' => '/ctrl/shop/articles',
            ),
            'shop-shipping' => array(
                'levels' => array('1'),
                'title' => 'Spedizione',
                'url' => '/ctrl/shop/shipping',
            ),
            'shop-orders' => array(
                'levels' => array('1'),
                'title' => 'Ordini',
                'url' => '/ctrl/shop/orders',
            ),
        ),
    ),
    'booking' => array(
        'levels' => array('1'),
        'title' => 'Booking',
        'icon' => 'fa-calendar',
        'sub' => array(
            'booking-main' => array(
                'levels' => array('1'),
                'title' => 'Tariffe e Disponibilità',
                'sub' => array(
                    'booking-overview' => array(
                        'levels' => array('1'),
                        'title' => 'Panoramica',
                        'url' => '/ctrl/booking/overview',
                    ),
                    'booking-setup' => array(
                        'levels' => array('1'),
                        'title' => 'Setup Camere / Tariffe',
                        'url' => '/ctrl/booking/setup',
                    ),  
                    'booking-prices' => array(
                        'levels' => array('1'),
                        'title' => 'Modifica Prezzi',
                        'url' => '/ctrl/booking/prices',
                    ),
                    'booking-availability' => array(
                        'levels' => array('1'),
                        'title' => 'Modifica Disponibilità',
                        'url' => '/ctrl/booking/availability',
                    ),
                    'booking-openclose' => array(
                        'levels' => array('1'),
                        'title' => 'Apri / Chiudi Camere',
                        'url' => '/ctrl/booking/openclose',
                    ),
                    'booking-restrictions' => array(
                        'levels' => array('1'),
                        'title' => 'Restrizioni',
                        'url' => '/ctrl/booking/restrictions',
                    ),
                    'booking-rates' => array(
                        'levels' => array('1'),
                        'title' => 'Tariffe',
                        'sub' => array(),
                    ),
                ),
            ),
            'booking-promo' => array(
                'levels' => array('1'),
                'title' => 'Promozioni',
                'url' => '/ctrl/booking/promo',
            ),
            'booking-reservations' => array(
                'levels' => array('1'),
                'title' => 'Prenotazioni',
                'url' => '/ctrl/booking/reservations',
            ),
        ),
    ),
    'users' => array(
        'levels' => array('1'),
        'title' => 'Utenti',
        'icon' => 'fa-users',
        'sub' => array(
            'customers' => array(
                'levels' => array('1'),
                'title' => 'Clienti',
                'url' => '/ctrl/customers',
            ),
            'agents' => array(
                'levels' => array('1'),
                'title' => 'Agenti',
                'url' => '/ctrl/agents',
            ),
        ),
    ),
);

As you can see, [sub]s can be nested
The question is still that: given a string matching one of the url in the above example, I would like to build the breadcrumbs using the titles

I.e, given the string /ctrl/booking/prices/ I'd like to get the following list (I can then convert to breadcrumb with CSS):

  1. Booking
  2. Tariffe e Disponibilità
  3. Modifica Prezzi

(you see I go backward in the array, finding titles; note that last item is linked to url)

Please, any help?

2

1 Answer 1

1

I edited your answer:

function getTitle(Array $arr, $find, $firstLevel = true) {
    $resultArray = array();
    foreach($arr as $val){
        if (isset($val['url']) && $val['url'] == $find) {
            return array('<li><a href="' . $val['url'] . '">' . $val['title'] . '</a></li>');
        }
        if (isset($val['sub'])) {
            $result = getTitle( $val['sub'], $find, false);
            if($result){
                $resultArray = array_merge($result);
                $resultArray[] = '<li>' . $val['title'] . '</li>';
                if(!$firstLevel){
                    return $resultArray;
                }
            }
        }
    }
    if(count($resultArray)){
        return implode(array_reverse($resultArray));
    }
    return false;
};

Example of usage:

echo getTitle($sidebar, '/ctrl/booking/prices');

Result:

  • Booking
  • Tariffe e Disponibilità
  • Modifica Prezzi
  • Example of usage 2:

    echo getTitle($sidebar, '/ctrl/categories');
    

    Result 2:

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

    1 Comment

    Davide, you just saved my day, or - supposing you are italian - "mi hai appena salvato il c..." :-) Thanks a lot!

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.