2011-03-12 65 views
1

的返回值我有一個數組的數組在它:PHP - 內的陣列和陣列

Array (
    [0] => Array (
    [qty] => 2 
    [name] => thing 1 
    [model] => 70001 
    [price] => 129.00 
    [weight] => 75.00 
    [id] => 139 
) 
    [1] => Array (
    [qty] => 1 
    [name] => thing 2 
    [model] => 70002 
    [price] => 199.00 
    [weight] => 45.00 
    [id] => 53 
) 
) 

我需要拉每個產品的重量把它比作一個MySQL數據庫(我可以做到這一點)乘以數量並返回成本;然後對下一個進行相同的操作等等。

最後更新(有完整的類功能):

function quote($method = '') { 
     global $order, $cart, $shipping_weight, $shipping_num_boxes; 

     $order->delivery['postcode'] = strtoupper($order->delivery['postcode']); 
     $order->delivery['postcode'] = str_replace (' ', '', $order->delivery['postcode']); 

    foreach($order->products as $value){//loop through arrays within arrays 
     $weight = $value['weight']; 
     $qty = $value['qty']; 
     $id = $value['id']; 

     if($weight <= 25)//find rate 
      $weight_class = 'w25'; 
     elseif(25 < $weight && $weight <= 50) 
      $weight_class = 'w50'; 
     elseif(50 < $weight && $weight <= 100) 
      $weight_class = 'w100'; 
     elseif(100 < $weight && $weight <= 150) 
      $weight_class = 'w150'; 
     else 
      $weight_class = 'w300'; 

     //strip postal code to fsa 
     $postalcode = strtoupper(substr($order->delivery['postcode'],0,3)); 
     //query database for cost 
     $postal_query = 'SELECT ' . $weight_class . ' FROM postal_codes WHERE fsa = "' . $postalcode . '"'; 
     $result = mysql_query($postal_query) or die(mysql_error()); 

     while($row = mysql_fetch_array($result)){ 
      $rate = $row[$weight_class].'<br>'; 
     } 
     $cost = $rate * $qty;//multiple by number of products 
     $shipping_total[$id] = $cost;//add total cost for this product(s) to array 
    } 
    $shipping_rate = array_sum($shipping_total);//sum array to total(s) 

     $this->quotes = array( 'id' => $this->code, 
           'module' => MODULE_SHIPPING_DLY3_TEXT_TITLE, 
           'methods' => array(array( 'id' => $this->code, 
                  'title' => MODULE_SHIPPING_DLY3_TEXT_WAY, 
                  'cost' => $shipping_rate))); 
     if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title); 
     if ($this->tax_class > 0) { 
      $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']); 
     } 
     return $this->quotes; 
    } 

如果我想出一個辦法來減少查詢的次數我會更新..

傑西

+1

聽起來你最終會對循環中的每個項目做一個查詢。這是低效的,你應該減少查詢。爲什麼用於構建數組的初始查詢無法自行完成此操作? – 2011-03-12 00:49:39

+0

該數組實際上是由框架的另一個模塊(它的OSC)構建的另一個數組,我對創建類不熟悉它。 – taylorjes 2011-03-12 00:57:26

回答

3
//Go over all products 
foreach($array as $value){ 
    $weight = $value['weight']; 
    // query goes here 
} 
+0

這是幫助。當我靠近時我會編輯 – taylorjes 2011-03-12 01:05:02

+0

我最近的更新正在工作...我將嘗試找到以減少查詢... – taylorjes 2011-03-13 09:48:13

1

我覺得我不明白你在問什麼,但爲什麼不試試

foreach($items as $id => $item) 
{ 
    $cost = $item['weight'] * $item['qty']; 
} 
+0

-1您完全忽略了從數據庫中檢索「rate」的要求。 – 2011-03-12 01:50:28