PHP: 从多维数组创建面包屑

PHP: create breadcrumbs from multimensional array

我有一个这样的多维数组:

$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/',
            ),
        )
    ),
);

我用它来创建我的(嵌套)侧边栏菜单

现在,给定一个与上面示例中的 url 之一匹配的字符串,我想使用 titles

构建面包屑

即,给定字符串 /ctrl/booking/prices/ 我想获得以下列表(然后我可以使用 CSS 转换为面包屑):

  1. 预订
  2. 关税和分配
  3. Modifica Prezzi

(你看我在数组中倒退,找到标题;注意最后一项链接到 url

我真的坚持下去了,这超出了我的技能:-(
提前致谢!

你可以这样试试

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

编辑后的版本

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