将 PHP 数组转换为 HTML 面包屑

发布于 2024-12-20 15:45:08 字数 1406 浏览 3 评论 0原文

我想将以下 php 数组: 转换

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

为 html 面包屑,如下所示:

<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>

任何人请告诉我如何在 php 中执行此操作。谢谢 !!

更新:感谢您的帮助。以下是对我有用的:

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

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>\n<li><a href=\"/\">Home</a>  ></li>\n");
foreach ($crumbs as $atext => $aloc) {
    if ($dotname != 'index.php' && $aloc == end($crumbs)) {
        $url .= '/'.$aloc;
        echo("<li><a href=\"$url\">$atext</a></li>\n");
    } else {
        $url .= '/'.$aloc;
        echo("<li><a href=\"$url/\">$atext</a>  ></li>\n");
    }
}
echo('</ul>');
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一片旧的回忆 2024-12-27 15:45:08

我会使用 foreach 和变量来记住路径。我将在几秒钟内举出一个例子。

可以在这里找到工作!

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!

心房的律动 2024-12-27 15:45:08

是这样的吗?:

<ul>
<?php
foreach($crumb as $name=>$href){
   echo "<a href=/'$href'>$name</a>"; 
}

?>

Somthing like that?:

<ul>
<?php
foreach($crumb as $name=>$href){
   echo "<a href=/'$href'>$name</a>"; 
}

?>

思念满溢 2024-12-27 15:45:08
<ul>
<li><a href="/">Home</a></li>
<?php
$path = '/';
foreach($crumbs as $name => $href) {
    echo '<li><a href="'.$path.$href.'/"></li>';
    $path .= $name.'/';
}
?>
</ul>
<ul>
<li><a href="/">Home</a></li>
<?php
$path = '/';
foreach($crumbs as $name => $href) {
    echo '<li><a href="'.$path.$href.'/"></li>';
    $path .= $name.'/';
}
?>
</ul>
貪欢 2024-12-27 15:45:08

你可以用这个

<?php
function ConvertPHPArrayToBreadcrumb($arr) {
    //convert the php array to html breadcrumb as required in 
    //http://stackoverflow.com/questions/8464019/convert-php-array-to-html-breadcrumbs
    $crmbs = '<ul> <li><a href="/">Home</a></li>';
    foreach ($arr as $key => $val) {
        $crmbs .= '<li><a href="' . $val . '/">' . $key . '</a></li>';
    }
    $crmbs .= "</ul>";
    return $crmbs;
}


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

//call the convert function
$html_crumbs = ConvertPHPArrayToBreadcrumb($crumbs);

//echo the reuls
echo($html_crumbs);
?>

you can use this

<?php
function ConvertPHPArrayToBreadcrumb($arr) {
    //convert the php array to html breadcrumb as required in 
    //http://stackoverflow.com/questions/8464019/convert-php-array-to-html-breadcrumbs
    $crmbs = '<ul> <li><a href="/">Home</a></li>';
    foreach ($arr as $key => $val) {
        $crmbs .= '<li><a href="' . $val . '/">' . $key . '</a></li>';
    }
    $crmbs .= "</ul>";
    return $crmbs;
}


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

//call the convert function
$html_crumbs = ConvertPHPArrayToBreadcrumb($crumbs);

//echo the reuls
echo($html_crumbs);
?>
千と千尋 2024-12-27 15:45:08
function showcrumbs($crumbs) {
    echo '<ul><li><a href="/">Home</a></li>';
    $path='/';
        foreach($crumbs as $name=>$href){
            echo '<li><a href="'.$path.$href.'">'.$name.'</a></li>'; 
            $path = $path.$href.'/';
        }
    echo '</ul>';
}

用法:

<?php
function showcrumbs($crumbs) {
    echo '<ul><li><a href="/">Home</a></li>';
    $path='/';
        foreach($crumbs as $name=>$href){
            echo '<li><a href="'.$path.$href.'">'.$name.'</a></li>'; 
            $path = $path.$href.'/';
        }
    echo '</ul>';
}
$crumbs = array(
'Financial Accounting'  => 'financial',
'Ratio Analysis'        => 'ratios',
'Current Ratio'         => 'current-ratio'
);
showcrumbs($crumbs);
?>
function showcrumbs($crumbs) {
    echo '<ul><li><a href="/">Home</a></li>';
    $path='/';
        foreach($crumbs as $name=>$href){
            echo '<li><a href="'.$path.$href.'">'.$name.'</a></li>'; 
            $path = $path.$href.'/';
        }
    echo '</ul>';
}

Usage:

<?php
function showcrumbs($crumbs) {
    echo '<ul><li><a href="/">Home</a></li>';
    $path='/';
        foreach($crumbs as $name=>$href){
            echo '<li><a href="'.$path.$href.'">'.$name.'</a></li>'; 
            $path = $path.$href.'/';
        }
    echo '</ul>';
}
$crumbs = array(
'Financial Accounting'  => 'financial',
'Ratio Analysis'        => 'ratios',
'Current Ratio'         => 'current-ratio'
);
showcrumbs($crumbs);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文