PHP - 在数组中返回数组的值

标签 php mysql loops foreach

我有一个包含数组的数组:

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;
    }

如果我找到减少查询数量的方法,我会更新..

杰西

最佳答案

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

关于PHP - 在数组中返回数组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5279959/

相关文章:

android - 从 MySQL 连接的 ID 属性设置 ListView ID

javascript - 为什么 php 在打印数据时会在客户端断开连接时中止,但在其他情况下不会中止?

javascript - 动态打开的下拉菜单

php - 我的个人资料页面,更新详细信息 PHP MYSQL,验证唯一性逻辑

php - 抛出 SQL 语法错误的简单查询

php - 推荐一个 php 验证框架

指定 FK 时,MYSQL 错误 #1072 在表中不存在

excel - 执行直到循环以获取大于某个数字的单元格的值

php - I18n 使用 PHP gettext()

php - 如何在处理不同的 If/Else block 时使下拉菜单停留?