带导航的数组打印出链接

时间:2014-04-26 18:22:37

标签: php arrays

<?php
$links = array(

    "Design" => "http://someurlhere.com", 
    "Marketing" => "http://someurlhere.com",
    "Content" => "http://someurlhere.com", 
    "Art" => "http://someurlhere.com", 
    "Video" => "http://someurlhere.com", 
    "Innovation" => "http://someurlhere.com", 
    "Technology" => "http://someurlhere.com", 
    "Legal" => "http://someurlhere.com", 
    "Accounting" => "http://someurlhere.com", 
);


// recursive function to build navigation list
function buildNav($links)
{

   echo "<ul>";

   foreach ($links as $link) {
       echo "<li>$link</li>";
   }

   echo "</ul>";
}

buildNav($links);
?>

我打印了网址,但它们不是链接而不是标题,例如设计打印为http://someurlhere.com,任何人都可以帮助打印标题,例如设计为链接名称,然后在单击它们时链接URL。

1 个答案:

答案 0 :(得分:1)

只需像其他任何链接一样添加<a>即可。这里的关键是在foreach loop中使用foreach ($array as $key => $value)语法:

foreach ($links as $link => $url) {
    echo "<li><a href='$url'>$link</a></li>";
}