在實際開發中有一個打亂數組排序的案例
案例代碼如下
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()函數對數據排序進行亂序處理即可