2012-11-24 76 views
1

得到的值時,在PHP中,我有我的代碼是這樣的PHP的示值誤差從數組

$discounts = $this->model_catalog_product->getProductDiscounts($product_id); 
     $product_discounts[] = array(); 
     foreach($discounts as $discount) { 
     $product_discounts[] = array(
      'quantity' => $discount['quantity'], 
      'price' => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax'))) 
     ); 
     } 

現在,當我提出這表明我的數組這樣

Array ([0] => Array ([product_discount_id] => 483 [product_id] => 43 [customer_group_id] => 8 [quantity] => 2 [priority] => 1 [price] => 345.0000 [date_start] => 0000-00-00 [date_end] => 0000-00-00) [1] => Array ([product_discount_id] => 484 [product_id] => 43 [customer_group_id] => 8 [quantity] => 4 [priority] => 2 [price] => 784.0000 [date_start] => 0000-00-00 [date_end] => 0000-00-00) [2] => Array ([product_discount_id] => 485 [product_id] => 43 [customer_group_id] => 8 [quantity] => 5 [priority] => 3 [price] => 786.0000 [date_start] => 0000-00-00 [date_end] => 0000-00-00)) 

但是當我做我的代碼從

<?php foreach ($discounts as $discount) { 
    echo sprintf($text_discount, $discount['quantity'], $discount['price']); ?> 
    } ?> 

我喜歡t的誤差陣列獲得折扣數量和折扣價格的值他Undefined variable: discounts

回答

1

替換此

$product_discounts[] = array(); 

$product_discounts = array(); 
+0

這實際上不是一個錯誤,並且不會回答這個問題。 –

0

$discounts從來沒有被定義。它應該是$product_discounts。這就是爲什麼你得到undefined variable discounts

<?php 
foreach ($product_discounts as $discount) { 
    echo sprintf($text_discount, $discount['quantity'], $discount['price']); ?> 
}?> 
0

試試這個

$discounts = $this->model_catalog_product->getProductDiscounts($product_id); 
    $product_discounts = array(); 
    foreach($discounts as $discount) { 
    $product_discounts = array(
     'quantity' => $discount['quantity'], 
     'price' => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax'))) 
    ); 
    }