Max Born Max Born - 7 months ago 55
PHP Question

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

Below is my breadcrumbs format, here I need to remove last » symbols and active last text of breadcrumbs and below is my php code and array .

Home » Mobiles & Accessories » Accessories »

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

Answer Source

Your array data:

<?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'
            ],
        ];
    ?>

Solution:

<?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;
    }
}
?>

This example has been tested on my local computer. It works.

Hope, this solution will help you.

Regards.