我会第一个承认PHP不是我的强项而且这个阵列开始让我疯狂。
这是我试图枚举的数组:
// array containing the site menu
$sitemap = array(
array( 'Title' => 'menu01',
'Description' => 'menu01_description',
'Address' => 'http://localhost/site.php?page=menu01',
),
array( 'Title' => 'menu02',
'Description' => 'menu02_description',
'Address' => 'http://localhost/site.php?page=menu02',
),
array( 'Title' => 'menu03',
'Description' => 'menu03_description',
'Address' => 'http://localhost/site.php?page=menu03',
)
);
现在我正在假设我需要使用foreach循环来枚举我的数组的内容。
这是我目前正在尝试实现的功能,但它一直返回一个错误,指出我的参数无效。
function GenerateMenu(){
$output = "<ul>";
foreach ($sitemap as $menuitem => $value){
if ($page == $value["Title"]){
$output .= '<li class="active">';
}
else {
$output .= '<li>';
}
$output .= '<a href="' . $value['Address'] . '">' . $value['Description'] . '</a><li>';
}
$output .= "</ul>";
return $output;
}
为什么我的论点无效?打印数组的最佳方法是什么?
答案 0 :(得分:3)
您忘了指定/通过$page
和$sitemap
。这些变量来自哪里?
尝试将其传递给您的函数:
function GenerateMenu($sitemap, $page) {
// …
}
然后你可以这样称呼它:
$sitemap = array(
// …
);
$page = 'menu01'
echo GenerateMenu($sitemap, $page);
答案 1 :(得分:0)
函数GenerateMenu()
需要发送$page
和$sitemap
的值,或者它不知道该怎么做。