• 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()函数对数据排序进行乱序处理即可

  • 相关阅读:
    内存对齐
    类和对象
    C++ 各种继承方式的类内存布局
    静态变量static
    程序内存布局
    String类
    C++与C的区别
    命名空间
    C
    D
  • 原文地址:https://www.cnblogs.com/LiChen-789/p/10124029.html
Copyright © 2020-2023  润新知