views:

64

answers:

3

Hello,

I got this code from someone and it works very well, I just want to remove the link from the last element of the array:

//get rid of empty parts
$crumbs = array_filter($crumbs);

$result = array();
$path = '';
foreach($crumbs as $crumb){
    $path .= '/' . $crumb;
    $name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumb));
    $result[] = "<a href=\"$path\">$name</a>";

}

print implode(' > ', $result);

This will output for example: Content > Common > File

I just want a to remove the link from the last item - "File" to be just plain text.. I tried myself to count the array items and then if the array item is the last one then to print as plain text the last item.. but I'm still noob, I haven't managed to get a proper result..

Thank you!

+2  A: 

This should work:

$crumbs = array_filter($crumbs);

$result = array(); $path = '';
//might need to subtract one from the count...
$count = count($crumbs);
foreach($crumbs as $k=>$crumb){
     $path .= '/' . $crumb;
     $name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumb));
     if($k != $count){
         $result[] = "<a href=\"$path\">$name</a>";
     } else {
         $result[] = $name;
     }
}

print implode(' > ', $result);
SeanJA
It's perfect thank you!
Adrian M.
assuming that your array is indexed 0 -> N
SeanJA
+1  A: 

You could simply tweak your existing code to use a 'normal' loop (rather than a foreach iterator) to achieve this.

For example:

//get rid of empty parts
$crumbs = array_filter($crumbs);

$result = array();
$path = '';
$crumbCount = count($crumbs);
for($crumbLoop=0; $crumbLoop<$crumbCount; $crumbLoop++) {
    $path .= '/' . $crumbs[$crumbLoop];
    $name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumbs[$crumbLoop]));
    $result[] = ($crumbLoop != $crumbCount -1) ? "<a href=\"$path\">$name</a>" : $name;
}

print implode(' > ', $result);

N.B: I don't have access to PHP at the moment, so the above might not be error free, but you should get the idea.

middaparka
lies! (15 chars)
SeanJA
A: 
for($i=0;$i< sizeof($crumbs);$i++) {
   $path .= '/' . $crumbs[$i];
   $name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumbs[$i]));

   if ($i != sizeof($crumbs)-1) {
      $result[] = "<a href=\"$path\">$name</a>";
   }else {
      $result[] = $name;
   }
}
Ayrton