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

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

函數不會在內部運行兩次 - if else - 語句

函數不會在內部運行兩次 - if else - 語句

PHP
海綿寶寶撒 2021-06-01 12:51:05
我想做一個條形碼查找。第一次嘗試時,會在用戶條形碼上本地查找條形碼。如果 是重復的,則該函數返回第一個實例,并帶有文本“在本地找到”如果在本地找不到條形碼,則應進行全局搜索。當在本地找不到條形碼時,我遇到的問題是 if else 語句。如果在本地找不到它,它應該檢查是否在全局范圍內找到它。public function updatePricelist(Request $request)    {        $user_id = Auth::user()->id;        $counter = count($_POST["product_name"]);        //product barcode        for ($x = 0; $x < $counter; $x++) {            $product_barcode[] = $_POST["product_barcode"][$x];        }        //check the barcode locally        $result = $this->barcodeLookUp($product_barcode, 'local');        if (!empty($result)) {           $result =  "found locally";        }        else {            $result = $this->barcodeLookUp($product_barcode, 'global');            if (!empty($result)) {                $result =  "found globally";            }        }        return $result;    }    public function barcodeLookUp($product_barcode, $type)    {        $user_id = Auth::user()->id;if ($type === 'local') {            $db_barcodes = $this->getBarcodes($user_id, 'local');        }        if ($type === 'global') {            $db_barcodes = $this->getBarcodes($user_id, 'global');        }        //merge arrays        $merge_array = array_merge($db_barcodes, $product_barcode);        function array_dup($ar)        {            return array_unique(array_diff_assoc($ar, array_unique($ar)));        }        $result = array_dup($merge_array);        return current($result);    }    public function getBarcodes($user_id, $type)    {        if ($type === 'local') {            $result = DB::select('SELECT products FROM vendor_pricelist WHERE user_id = ?', [$user_id]);        }        if ($type === 'global') {            $result = DB::select('SELECT products FROM vendor_pricelist WHERE user_id != ?', [$user_id]);        }        $count_results = count($result);        $temp = [];        $temp_2 = [];        for ($x = 0; $x < $count_results; $x++) {            array_push($temp, json_decode($result[$x]->products));        }
查看完整描述

2 回答

?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

這可能有幫助:


    //check the barcode locally

    $result = $this->barcodeLookUp($product_barcode, 'local');


    if (empty($result)) {

        $result = $this->barcodeLookUp($product_barcode, 'global');


        if (!empty($result)) {

            return [

                "message" => "found globally",

                "result"  => $result,

            ];

        }


        return [

            "message" => "not found",

            "result"  => null,

        ];

    } else {

        return [

            "message" => "found locally",

            "result"  => $result,

        ];

    }


查看完整回答
反對 回復 2021-06-04
  • 2 回答
  • 0 關注
  • 189 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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