1

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!

1
  • What you're looking for is basically the array_search function for multdimensional arrays. This topic describes some options: stackoverflow.com/questions/7694843/… Commented Oct 9, 2015 at 11:53

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This is almost fine, I edited as in my answer :-) But here comes a problem that I didn't show: there can be MANY nested [sub], and this complicates things...
0

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;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.