从数组中获取最高元素

时间:2014-08-18 13:48:15

标签: php arrays codeigniter cart

我有一个查询,它给了我一个添加到数据库中的折扣元组。

这是查询

$discount_info = $this->autoload_model->get_data_from_table("td_discount,td_userdiscount","*",
                                               "td_discount.discount_id = td_userdiscount.discount_id
                                                AND td_discount.discount_code = '$coupon'")->result_array();

现在我有执行特定功能的脚本。

如果索引的值为1,会有一个条件,那么代码片段就是这样的

if($discount_info[0]['discount_on']=="3")
    {
        $discount_product = $discount_info[0]['discount_product']; // its an id(autoincrement value)//
        if($discount_info[0]['applicable_type']==1)
        {
            $item_info = $this->autoload_model->get_data_From_table("td_product","*","product_id = '$discount_product'")->result_array();
            foreach($this->cart->contents() as $ci)
            {
                if($ci['name'] = $item_info[0]['product_name']
                {
                    // get the cart_item with the highest price if the product name matches//
                }
            }
        }
    }

我的购物车结构是这样的

$data = array(
                'id'      => $id,
                'qty'     => $qty,
                'price'   => $price,
                'name'    => $name,
                'options' => array(
                                    'picture'=>$img,
                                    'item_slug'=>$slug,
                                    'item_color'=>$color,
                                    'item_size'=>$size,
                                    'unit_price'=>$price,
                                    'order_type'=>$order_type,
                                    'product_type'=>$pro_type,
                                    'unit_discount' => 0.00,
                                    'item_discount' => 0.00,
                                    'discount_type' => '',
                                    )
                );

现在,它全部设置完毕,但我无法获得我将在此处放置的登录信息

// get the cart_item with the highest price if the product name 

1 个答案:

答案 0 :(得分:2)

我想你可以定义一个

$highest = array('price' => 0);
在循环之前

然后在循环内转到:

// get the cart_item with the highest price if the product name matches//
if ($ci['price'] > $highest['price']) {
    $highest = $ci;
}

这样,$ high将包含最后的最佳匹配。