php 二維數組索引亂序 shuffle() 函數;


在實際開發中有一個打亂數組排序的案例  

案例代碼如下

 public function index(){
        if(IS_POST){
            #獲取賣單列表
            $buy_list=M('buy')->where(['status'=>1])->field('num')->select();
            #獲取賣單列表
            $sell_list=M('sell')->where(['status'=>1])->field('num')->select();
            foreach($buy_list as $key=>$value){
                $buy_list[$key]['type']='buy';
                $buy_list[$key]['rmb']=round($value['num']*6.3,2);
            }

            foreach($sell_list as $k=>$v){
                $sell_list[$k]['type']='sell';
                $sell_list[$k]['rmb']=round($v['num']*6.3,2);
            }

            $order=(array_merge($buy_list,$sell_list));
            shuffle($order);
            if($order){
                $this->ajaxReturn(['code'=>200,'data'=>$order]);
            }
            $this->ajaxReturn(['code'=>100,'msg'=>'暫時沒有訂單數據,您可以預下下單,系統會優先匹配']);
        }else{
            $this->display();
        }

在合並數組后只需要使用shuffle()函數對數據排序進行亂序處理即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM