php二維數組中如何對其中一維數組中的某個欄位進行排序

來源:互聯網
上載者:User

本篇文章給大家分享的內容是php二維數組中如何對其中一維數組中的某個欄位進行排序,有著一定的參考價值,有需要的朋友可以參考一下

這裡擷取到的資料庫裡的資料

public function hot_sort(){ $type = input('type');  $list = $this->get_hot_sort($type);  $res = $list->data;  $re = $this->object_array($res);  foreach ($re as $k=>$v){  $data= Db::name('merchants')->field('share_num')->where(['gl_merchants_id'=>$v['user_id']])->find();  $re[$k]['share_num'] = $data['share_num'];  }  $result = $this->quick_sort($re); //這裡調用方法根據某個欄位進行排序 success($result);  }

========================================================================

方法進行排序 /**     * @param $arrUsers     * @return mixed     *二維數組某個欄位進行排序     */    function quick_sort($arrUsers)    {        $sort = array(                    'direction' => 'SORT_DESC', //排序次序標誌 SORT_DESC 降序;SORT_ASC 升序              'field'     => 'share_num',       //排序欄位        );                $arrSort = array();                foreach($arrUsers AS $k => $v){                    foreach($v AS $key=>$value){                              $arrSort[$key][$k] = $value;            }        }                if($sort['direction']){            array_multisort($arrSort[$sort['field']], constant($sort['direction']), $arrUsers);        }                        return $arrUsers;    }原來的排序結果{    "status": "ok",      "data": [        {                    "user_id": "29",                          "shop_id": 7,                          "total_money": 40000,                          "user_name": "b1",                          "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg",            "share_num": ""        },        {                    "user_id": "16",                          "shop_id": 6,                          "total_money": 15000.01,                          "user_name": "b7",                          "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20180122/1516586013148395.jpg!120x120.jpg",            "share_num": ""        },        {                    "user_id": "13",                          "shop_id": 4,                          "total_money": 5000,                          "user_name": "gelu1234",                          "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg",            "share_num": ""        },        {            "user_id": "56",                                 "shop_id": 20,                                 "total_money": 70,                                 "user_name": "HECAI",                                 "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg",            "share_num": ""        },        {                    "user_id": "32",                         "shop_id": 9,                         "total_money": 37.1,                         "user_name": "baxianqiao",                         "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg",            "share_num": "670000"        },        {                    "user_id": "8",                         "shop_id": 1,                         "total_money": 0,                         "user_name": "SXMY",                         "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg",            "share_num": ""        },        {                    "user_id": "35",                         "shop_id": 8,                         "total_money": 0,                         "user_name": "self01",                                          "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20180122/1516586013148395.jpg!120x120.jpg",            "share_num": ""        }    ]}現在排序的結果{    "status": "ok",    "data": [        {                    "user_id": "32",                         "shop_id": 9,                         "total_money": 37.1,                         "user_name": "baxianqiao",                         "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg",            "share_num": "670000"        },        {                    "user_id": "8",                          "shop_id": 1,                          "total_money": 0,                          "user_name": "SXMY",                          "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg",            "share_num": ""        },        {                    "user_id": "13",                         "shop_id": 4,                         "total_money": 5000,                         "user_name": "gelu1234",                         "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg",            "share_num": ""        },        {                     "user_id": "16",                         "shop_id": 6,                         "total_money": 15000.01,                         "user_name": "b7",                         "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20180122/1516586013148395.jpg!120x120.jpg",            "share_num": ""        },        {                   "user_id": "29",                          "shop_id": 7,                          "total_money": 40000,                          "user_name": "b1",                          "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg",            "share_num": ""        },        {                    "user_id": "35",                         "shop_id": 8,                         "total_money": 0,                         "user_name": "self01",                         "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20180122/1516586013148395.jpg!120x120.jpg",            "share_num": ""        },        {                    "user_id": "56",                          "shop_id": 20,                          "total_money": 70,                          "user_name": "HECAI",                          "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg",            "share_num": ""        }    ]}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.