2014-02-21 164 views
1

它是Virtuemart,但它更基本的PHP的東西...顯示最後一個數組元素

我需要一些幫助/提示...

我想要做什麼 - 我想顯示TAXAMOUNT總和只有在Virtuemart購物車中有一些ID的折扣(這是 - >我想每個折扣類型顯示單獨的折扣)。

這裏是我想到目前爲止:

foreach ($this->cart->products as $pkey => $prow) 
{ 
if ($prow->product_discount_id==2) 
{ 
    $discounts= $this->cart->pricesUnformatted[$pkey]['discountAmount']; 
    $discountss+=$discounts; 
    echo "<span class='priceColor2'>" . $discountss . "</span>" ; 
} 
} 

轉Bt基因的問題是,它echo'ing所有優惠總和(同時的foreach是真的)......我怎麼能只顯示最後一個元素或也許最好使用不同的解決方案

TNX!

+0

我將從PHP手冊開始http://us2.php.net/array_pop – Dave

回答

-1

如果你想顯示數組中的最後一個元素,你可以使用end()。例如:

$fruits = array('apple', 'banana', 'cranberry'); 
echo end($fruits); // cranberry 
-1

不是100%肯定我關注你,但你可以使用array_pop()返回數組的最後一個元素。

$myArray = array('apple', 'banana', 'orange'); 
$fruit = array_pop($myArray); 

echo $fruit; //echoes 'orange' 

有關更多信息,請參閱manual

0

你應該把echo帶出foreach循環。

$discountss = 0; 
foreach ($this->cart->products as $pkey => $prow) { 
    if ($prow->product_discount_id==2) { 
     $discountss += $this->cart->pricesUnformatted[$pkey]['discountAmount']; 
    } 
} 

echo "<span class='priceColor2'>" . $discountss . "</span>" ; 
+0

這是解決方案! TNX!愚蠢的錯誤...當頭已經超載時,永遠不應該工作! ;-D –

相關問題