如何从面包屑中删除最后一个 » 并在 php 中激活最后一个文本?

How remove last » from breadcrumbs and active last text in php?

下面是我的面包屑格式,这里我需要删除面包屑的最后一个»符号和活动的最后一个文本,下面是我的php代码和数组。

主页 » 手机和配件 » 配件 »

<div class="txt">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a> &raquo; 
<?php } ?>
</div>

Array
(
[0] =&gt; Array
(
[text] =&gt; Home
[href] =&gt; http://localhost/opencart/index.php?route=common/home
)
[1] =&gt; Array
(
[text] =&gt; Mobiles &amp; Accessories
[href] =&gt; http://localhost/opencart/index.php?route=product/category&amp;path=33
)
[2] =&gt; Array
(
[text] =&gt; Accessories
[href] =&gt; http://localhost/opencart/index.php?route=product/category&amp;path=33_62
)
)

你的数组数据:

<?php

    $breadcrumbs =
        [
            [
                'text' => 'Home',
                'href' => 'http://localhost/opencart/index.php?route=common/home'
            ],
            [
                'text' => 'Mobiles &amp; Accessories',
                'href' => 'http://localhost/opencart/index.php?route=product/category&amp;path=33'
            ],
            [
                'text' => 'Accessories',
                'href' => 'http://localhost/opencart/index.php?route=product/category&amp;path=33_62'
            ],
        ];
    ?>

解决方案:

<?php
$breadcrumbsCount = count($breadcrumbs);
for ($i = 0; $i < $breadcrumbsCount; $i++) {
    if ($i != $breadcrumbsCount - 1) {
        echo '<a href="'.$breadcrumbs[$i]['href'].'">'.$breadcrumbs[$i]['text'].'</a> &raquo;' . PHP_EOL;
    } else {
        echo '<a href="'.$breadcrumbs[$i]['href'].'" class="active">'.$breadcrumbs[$i]['text'].'</a>' . PHP_EOL;
    }
}
?>

此示例已在我的本地计算机上进行了测试。有效。

希望此解决方案对您有所帮助。

此致。