PHP 动态面包屑 - 如何隐藏重复项?
PHP Dynamic Breadcrumbs - How To Hide Duplicates?
我正在尝试删除动态面包屑中的重复项。
//$url = substr(strstr($_SERVER["REQUEST_URI"], 'admin'), 6);
$url = substr(strstr('/content/modal/admin/content/timeline/timeline', 'admin'), 6);
$parse_url = substr($url, 0, strpos($url, "."));
$array = explode("/", $parse_url);
if($array[0] == 'index') {
echo 'CMP';
} else {
echo '<a href="javascript:;" onclick="initializemodal(\'content/modal/admin/index\')">Staff</a> / ';
$path = [];
foreach($array as $value) {
$path[] = $value;
echo '<a href="javascript:;" onclick="initializemodal(\'content/modal/admin/' . implode('/', $path) . '\')">' . $value . '</a> / ';
}
}
这输出为:
<a href="javascript:;" onclick="initializemodal('content/modal/admin/index')">Staff</a> /
<a href="javascript:;" onclick="initializemodal('content/modal/admin/content')">content</a> /
<a href="javascript:;" onclick="initializemodal('content/modal/admin/content/timeline')">timeline</a> /
<a href="javascript:;" onclick="initializemodal('content/modal/admin/content/timeline/timeline')">timeline</a> /
如您所见,它还在第 3 行列出了目录。第四行是文件本身。
我怎样才能让它在 URL 中保留副本,但跳过 link 本身?澄清一下,我怎样才能得到这个输出?:
<a href="javascript:;" onclick="initializemodal('content/modal/admin/index')">Staff</a> /
<a href="javascript:;" onclick="initializemodal('content/modal/admin/content')">content</a> /
<a href="javascript:;" onclick="initializemodal('content/modal/admin/content/timeline/timeline')">timeline</a> /
将循环更改为:
foreach($array as $index => $value) {
$path[] = $value;
//skip current element if next element is identically
if($value == $array[$index+1]) continue;
echo '<a href="javascript:;" onclick="initializemodal(\'content/modal/admin/' . implode('/', $path) . '\')">' . $value . '</a> / ';
}
如果下一个元素相同,它将跳过数组中的一个元素。
所以当有重复的条目时(但只是紧随其后),只有最后一个条目(和他的完整url)被打印出来
我正在尝试删除动态面包屑中的重复项。
//$url = substr(strstr($_SERVER["REQUEST_URI"], 'admin'), 6);
$url = substr(strstr('/content/modal/admin/content/timeline/timeline', 'admin'), 6);
$parse_url = substr($url, 0, strpos($url, "."));
$array = explode("/", $parse_url);
if($array[0] == 'index') {
echo 'CMP';
} else {
echo '<a href="javascript:;" onclick="initializemodal(\'content/modal/admin/index\')">Staff</a> / ';
$path = [];
foreach($array as $value) {
$path[] = $value;
echo '<a href="javascript:;" onclick="initializemodal(\'content/modal/admin/' . implode('/', $path) . '\')">' . $value . '</a> / ';
}
}
这输出为:
<a href="javascript:;" onclick="initializemodal('content/modal/admin/index')">Staff</a> /
<a href="javascript:;" onclick="initializemodal('content/modal/admin/content')">content</a> /
<a href="javascript:;" onclick="initializemodal('content/modal/admin/content/timeline')">timeline</a> /
<a href="javascript:;" onclick="initializemodal('content/modal/admin/content/timeline/timeline')">timeline</a> /
如您所见,它还在第 3 行列出了目录。第四行是文件本身。
我怎样才能让它在 URL 中保留副本,但跳过 link 本身?澄清一下,我怎样才能得到这个输出?:
<a href="javascript:;" onclick="initializemodal('content/modal/admin/index')">Staff</a> /
<a href="javascript:;" onclick="initializemodal('content/modal/admin/content')">content</a> /
<a href="javascript:;" onclick="initializemodal('content/modal/admin/content/timeline/timeline')">timeline</a> /
将循环更改为:
foreach($array as $index => $value) {
$path[] = $value;
//skip current element if next element is identically
if($value == $array[$index+1]) continue;
echo '<a href="javascript:;" onclick="initializemodal(\'content/modal/admin/' . implode('/', $path) . '\')">' . $value . '</a> / ';
}
如果下一个元素相同,它将跳过数组中的一个元素。 所以当有重复的条目时(但只是紧随其后),只有最后一个条目(和他的完整url)被打印出来