Ivan
Ivan

Reputation: 2481

PHP: create breadcrumbs from multimensional array

I have a multidimensional array like this:

$sidebar_booking = array(
    'booking' => array(
        'levels' => array('1'),
        'title' => 'Booking',
        'icon' => 'fa-calendar',
        'sub' => array(
            'rates-availability' => 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',
                    ),
                ),
            ),
            'booking-promo' => array(
                'levels' => array('1'),
                'title' => 'Promozioni',
                'url' => '/ctrl/booking/promo/',
            ),
            'booking-reservations' => array(
                'levels' => array('1'),
                'title' => 'Prenotazioni',
                'url' => '/ctrl/booking/reservations/',
            ),
        )
    ),
);

I use it to create my (nested) sidebar menu

Now, 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)

I'm really stuck on it, it's over my skills :-(
Thanks in advance!

Upvotes: 1

Views: 965

Answers (2)

Ivan
Ivan

Reputation: 2481

Edited version

function getTitle(Array $arr, $find) {
    foreach($arr as $val){
        if (isset($val['url']) && $val['url'] == $find) {
            return '<li><a href="' . $val['url'] . '">' . $val['title'] . '</a></li>';
        }
        if (isset($val['sub'])) {
            return '<li>' . $val['title'] . '</li>' . getTitle( $val['sub'], $find);
        }
    }
    return false;
};

Upvotes: 0

You can try like this

function getTitle(Array $arr, $find) {
    if (isset($arr['url']) && $arr['url'] == $find) {
         return $arr['title'];
    }
    if (isset($arr['sub'])) {
        return $arr['title'] . '/' . getTitle( $arr['sub'], $find);
    }
    return false;
}

echo getTitle($menu, $url);

Upvotes: 1

Related Questions