2014-02-21 61 views
0

你好,大家好,我有這樣的代碼:問題與陣列需要所以首先調整串

$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"; 

現在的問題是,我試圖刪除這部分代碼:

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

但僅僅從陣列中的最後一個項目,因此現在非常的像這樣得到的輸出:http://puu.sh/74ure.png

但我希望「>大單」,從爲「認可六的最後一項帶走DEO」。

+1

你的代碼的外觀上來看,它在$屑每個數組元素將有兩個環節一個來自哪裏array and Smile TRU「。你確定唯一的問題是最後一位 – Surace

回答

0

首先計算麪包屑,然後在循環中檢查您是否正在處理最後一個。前提是「cookie_crumbs」的數組索引數字形式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>"; 
    } 
} 
+0

已經工作了,謝謝。 – user3332590

1

試試這個:

$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"; 
+0

編輯答案請檢查一下 –