亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從php中的平板數組中獲取折扣值

如何從php中的平板數組中獲取折扣值

PHP
MMTTMM 2022-01-23 10:29:33
我正在嘗試使用預定義的板作為數組來獲取折扣數(值)。例如,如果總產品數量在 11 到 20 之間,那么我想返回 25 作為折扣值。這可能很簡單,但我不知道該怎么做。因為簡單的 foreach 循環可能不起作用。/** * The function returns the discount amount from the slabs * * @param int $products_count total number of product in cart * * @return mixed null|int returns discount value if matches else null */public function product_discounts($products_count){    $discount_slabs = [        '10' => '15',        '20' => '25',        '30' => '35',        '50' => '50',    ];    foreach ($discount_slabs as $count => $discount) {        if ($products_count <= $count) {            $this->discount = $discount;        }    }    return $this->discount;}
查看完整描述

1 回答

?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

您只需要在循環中返回折扣,這樣當您找到適合產品計數的最小平板時,您就可以停止查看后續值。否則所有值將返回 50%。如果您跳出循環,則用戶擁有超過 50 個產品,他們將獲得最后一個折扣值,這是最大的一個。像這樣的東西:


public function product_discounts($products_count)

{

    $discount_slabs = [

        '10' => '15',

        '20' => '25',

        '30' => '35',

        '50' => '50',

    ];


    // set the base discount

    $this->discount = 0;


    foreach ($discount_slabs as $count => $discount) {

        if ($products_count <= $count) {

            // if less than this bracket, return the current discount

            return $this->discount;

        }

        // otherwise, increase the discount level

        $this->discount = $discount;

    }

    return $this->discount = $discount;

}


查看完整回答
反對 回復 2022-01-23
  • 1 回答
  • 0 關注
  • 144 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號