简体   繁体   中英

Issue with array need to adjust string

Hello there guys so first of all, I have this code:

$crumbs = array();
$crumbs[] = "<a href=\"/\">Triple O Dental Laboratory</a>";

if (is_array($GLOBALS["cookie_crumbs"])) {
    foreach($GLOBALS["cookie_crumbs"] as $mycrumb) {
        $mycrumb[1] = str_replace("//","/",$mycrumb[1]);
        $crumbs[] = "<a href=\"".$mycrumb[1]."\">".$mycrumb[0]."</a> > <a href=\"#\">Smile TRU</a>";
    }
}

print "<div class=\"cookie_crumbs2\">\n";
print implode(" > ",$crumbs);
print "</div>\n";

Now the problem is, i am trying to remove this part of the code:

> <a href=\"http://stage.tripleodentallabs.com/laboratory/smile-tru/\">Smile TRU</a>";

But only from the LAST item in the array, so pretty much at the moment its getting output like this: http://puu.sh/74ure.png

But I want the " > big one" taken away from the last item which is "Accreditation Video".

Try this :

$crumbs = array();
$crumbs[] = "<a href=\"/\">Triple O Dental Laboratory</a>";

if (is_array($GLOBALS["cookie_crumbs"])) {
    foreach($GLOBALS["cookie_crumbs"] as $mycrumb) {
        if(end($GLOBALS["cookie_crumbs"] != $mycrumb)){
            $mycrumb[1] = str_replace("//","/",$mycrumb[1]);
            $crumbs[] = "<a href=\"".$mycrumb[1]."\">".$mycrumb[0]."</a> > <a href=\"#\">Smile TRU</a>";
        }
        else{
            $mycrumb[1] = str_replace("//","/",$mycrumb[1]);
            $crumbs[] = "<a href=\"".$mycrumb[1]."\">".$mycrumb[0]."</a>";
        }
    }
}

print "<div class=\"cookie_crumbs2\">\n";
print implode(" > ",$crumbs);
print "</div>\n";

First count the crumbs and then, in the loop, check if You are handling the last one or not. Provided that the "cookie_crumbs" is array indexed numerically form 0:

$last = count($GLOBALS["cookie_crumbs"]) - 1;

foreach ($GLOBALS["cookie_crumbs"] as $index => $mycrumb) {

    if ($index === $last) {

        $crumbs[] = 'I am the last one'; // do whatever You need here...
    }
    else {

        $mycrumb[1] = str_replace("//","/",$mycrumb[1]);
        $crumbs[] = "<a href=\"".$mycrumb[1]."\">".$mycrumb[0]."</a> > <a href=\"#\">Smile TRU</a>";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM