【发布时间】:2009-10-02 13:53:35
【问题描述】:
我会第一个承认 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;
}
为什么我的论点无效?打印数组的最佳方法是什么?
【问题讨论】:
-
$page 和 $sitemap 是在哪里定义的?它们不应该被传递到你的函数中吗?
-
哇;发布后大约 45 秒回答了我自己的问题。我没有将数组 $sitemap 传递给 GenerateMenu。该数组不在函数的范围内,因此没有数组 $sitemap 可以迭代。
标签: php multidimensional-array