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

时间:2015-10-12 10:13:18

标签: php arrays multidimensional-array breadcrumbs

在我的previous question我错误地只考虑了一个[sub]键,但整个侧边栏菜单是这样的:

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

如您所见,[sub]可以嵌套 问题仍然是:给定一个字符串匹配上面示例中的url之一,我想使用title s

构建面包屑

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

  1. 预订
  2. TariffeeDisponibilità
  3. Modifica Prezzi
  4. (你看我在数组中向后移动,查找标题;请注意,最后一项与url相关联)

    请帮忙吗?

1 个答案:

答案 0 :(得分:1)

我编辑了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;
};

使用示例

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

<强>结果

  • 预订
  • TariffeeDisponibilità
  • Modifica Prezzi
  • 使用示例2

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

    结果2

  • Impostazioni
  • Categorie