开发者

PHP - Return values of an array within and array

I have an array with arrays in it:

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
  )
)

I need to pull the weight of each product compare it to a MySQL DB (I can do this) multiply that by the qty and return a cost; then do the same to the next and so on.

Final Update (with complete class function):

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';
    开发者_如何学Go    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;
    }

I'll update if i figure out a way to reduce the number of queries..

Jesse


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


I feel like I'm not understanding what you're asking but why not try

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜