Home > Backend Development > PHP Tutorial > How to sort a field in one-dimensional array in php two-dimensional array

How to sort a field in one-dimensional array in php two-dimensional array

零到壹度
Release: 2023-03-23 15:22:01
Original
2142 people have browsed it

The content of this article is to share with you how to sort a certain field in one-dimensional array in PHP two-dimensional array. It has certain reference value. Friends in need can refer to it

The data in the database obtained here

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); 
 }
Copy after login

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

方法进行排序 /**
     * @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": ""
        }
    ]
}
Copy after login

The above is the detailed content of How to sort a field in one-dimensional array in php two-dimensional array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template