您好我有以下代码来生成导航/页面菜单以及将样式元素应用于活动页面。我想知道如何缩小代码而不是使用两个合并数组?
这是我目前的代码,两者之间的区别是html链接输出。 “主页”链接必须为<a href="./'. $k .'">'. $v .'</a>
而其他页面需要<a href="./?p='. $k .'">'. $v .'</a>
<?php
$current = array(
"" => "Home"
);
foreach( $current as $k => $v ) {
$active = $_GET['p'] == $k
? ' class="current_page_item"'
: '';
echo '<li'. $active .'><a href="./'. $k .'">'. $v .'</a></li>';
}
$current = array(
"contact" => "Contact Us",
"about" => "About Us",
"privacy" => "Privacy Policy"
);
foreach( $current as $k => $v ) {
$active = $_GET['p'] == $k
? ' class="current_page_item"'
: '';
echo '<li'. $active .'><a href="./?p='. $k .'">'. $v .'</a></li>';
}
?>
非常感谢任何帮助,谢谢:)
答案 0 :(得分:1)
<?php
$current = array(
"" => "Home"
"contact" => "Contact Us",
"about" => "About Us",
"privacy" => "Privacy Policy"
);
foreach( $current as $k => $v ) {
$active = $_GET['p'] == $k
? ' class="current_page_item"'
: '';
echo '<li'. $active .'><a href="./'.(empty($k)?"":"?p=") . $k .'">'. $v .'</a></li>';
}
?>
答案 1 :(得分:1)
差不多
$ href =空($ k)? “./”:“。/?p = {$ k}”;