PHP:从多维数组创建面包屑 [英] 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
现在,给定与上面示例中的 url
之一匹配的字符串,我想使用 title <构建面包屑/ code> s
Now, given a string matching one of the url
in the above example, I would like to build the breadcrumbs using the title
s
即,给定字符串 / ctrl / booking / prices /
想要获得以下列表(然后可以使用CSS转换为面包屑):
I.e, given the string /ctrl/booking/prices/
I'd like to get the following list (I can then convert to breadcrumb with CSS):
- 预订
- Tariffe eDisponibilità
- Modifica Prezzi b
- Booking
- Tariffe e Disponibilità
- Modifica Prezzi
(您看到我在数组中向后退,找到标题;请注意,最后一项链接到 url
)
(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!
推荐答案
您可以这样尝试
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);
这篇关于PHP:从多维数组创建面包屑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!