PHP一维数组、二维数组排序

1、一维数组

结构:

{
    "data": [
        {
            "id": 1,
            "name": "套餐一",
            "category": null,
            "type": 1,
            "short_introduction": "套餐介绍",
            "detail": "套餐详情",
            "price": 4500,
            "discount_price": null
        },
        {
            "id": 2,
            "name": "套餐二",
            "category": null,
            "type": 1,
            "short_introduction": "套餐介绍二",
            "detail": "套餐详情二",
            "price": 2300,
            "discount_price": null
        },
        {
            "id": 3,
            "name": "套餐三",
            "category": null,
            "type": 1,
            "short_introduction": "套餐三",
            "detail": null,
            "price": 2300,
            "discount_price": null
        },
        {
            "id": 4,
            "name": "套餐四",
            "category": null,
            "type": 1,
            "short_introduction": "套餐四",
            "detail": "套餐详情",
            "price": 1700,
            "discount_price": null
        }
    ],
    "meta": {
        "pagination": {
            "total": 4,
            "count": 4,
            "per_page": 10,
            "current_page": 1,
            "total_pages": 1,
            "links": []
        }
    }
}



$sort    = request()->input('sortby','id');
        $order   = request()->input('order','asc');
        $paginator = $this->packageService->with(includes())->paginate();


        $packages = fractal($paginator, new PackageTransformer())
            ->parseIncludes(includes())
            ->toArray();


        $results = [];
        if('price' == $sort) {


            foreach ($packages['data'] as $unique => $package) {
                $results[] = $package['price'];
            }
            if('asc' == $order) {


                array_multisort($results,SORT_ASC,$packages['data']);


            }else{


                array_multisort($results,SORT_DESC,$packages['data']);


            }


        }


        if (!$packages) {
            return error(\Msg::PACKAGE_NOT_FOUND);
        }


        return $this->response($packages);


2、二维数组

  1. $arrUsers = array(  
  2.     array(  
  3.             'id'   => 1,  
  4.             'name' => '张三',  
  5.             'age'  => 25,  
  6.     ),  
  7.     array(  
  8.             'id'   => 2,  
  9.             'name' => '李四',  
  10.             'age'  => 23,  
  11.     ),  
  12.     array(  
  13.             'id'   => 3,  
  14.             'name' => '王五',  
  15.             'age'  => 40,  
  16.     ),  
  17.     array(  
  18.             'id'   => 4,  
  19.             'name' => '赵六',  
  20.             'age'  => 31,  
  21.     ),  
  22.     array(  
  23.             'id'   => 5,  
  24.             'name' => '黄七',  
  25.             'age'  => 20,  
  26.     ),  
  27. );   
  28.   
  29.   
  30. $sort = array(  
  31.         'direction' => 'SORT_DESC'//排序顺序标志 SORT_DESC 降序;SORT_ASC 升序  
  32.         'field'     => 'age',       //排序字段  
  33. );  
  34. $arrSort = array();  
  35. foreach($arrUsers AS $uniqid => $row){  
  36.     foreach($row AS $key=>$value){  
  37.         $arrSort[$key][$uniqid] = $value;  
  38.     }  
  39. }  
  40. if($sort['direction']){  
  41.     array_multisort($arrSort[$sort['field']], constant($sort['direction']), $arrUsers);  
  42. }  
  43.   
  44. var_dump($arrUsers); 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值