douzhangcuo2174 2011-12-11 12:38
浏览 52
已采纳

将PHP数组转换为HTML面包屑

I want to convert the following php array:

$crumbs = array(
'Financial Accounting'  => 'financial',
'Ratio Analysis'        => 'ratios',
'Current Ratio'         => 'current-ratio'
);

to html breadcrumb as shown below:

<ul>
  <li><a href="/">Home</a></li>
  <li><a href="/financial/">Financial Accounting</a></li>
  <li><a href="/financial/ratios/">Ratios Analysis</a></li>
  <li><a href="/financial/ratios/current-ratio">Current Ratio</a></li>
</ul>

Anyone please show me how to do it in php. Thanks !!

UPDATE: Thanks for your help. Following is what worked for me:

<?php
$dotname = basename($_SERVER['PHP_SELF']);
$crumbs = array(
'Financial Accounting'  => 'financial',
'Ratio Analysis'        => 'ratios',
'Current Ratio'         => 'current-ratio'
);
echo("<ul>
<li><a href=\"/\">Home</a>&nbsp;&nbsp;&gt;</li>
");
foreach ($crumbs as $atext => $aloc) {
    if ($dotname != 'index.php' && $aloc == end($crumbs)) {
        $url .= '/'.$aloc;
        echo("<li><a href=\"$url\">$atext</a></li>
");
    } else {
        $url .= '/'.$aloc;
        echo("<li><a href=\"$url/\">$atext</a>&nbsp;&nbsp;&gt;</li>
");
    }
}
echo('</ul>');
?>
  • 写回答

5条回答 默认 最新

  • dousigan0499 2011-12-11 12:42
    关注

    I'd go with a foreach and a variable to remember the path. I'll whip up an example in a few seconds.

    A Working Can be Found Here!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误