php 多维数组 array sort 排序 :array_multisort

Posted cb_za

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 多维数组 array sort 排序 :array_multisort相关的知识,希望对你有一定的参考价值。

 

1.参考链接:

  php简单实现多维数组排序的方法

  参考二: 这个链接很好,可以直接看这个:PHP array_multisort—对多个数组或多维数组进行排序

 

 

2.案例一:

 

 1     //13: 最佳:
 2     public function zjService()
 3     {
 4         //小组得分:
 5         $team = array();
 6         //学生得分:
 7         $student = array();
 8 
 9         //取出所有的小组:
10         $TeamModel = new TeamModel();
11         $where["where"] = " 1 =1 order by orderBy asc, id asc ";
12         $data = $TeamModel->getDataList($where);
13 
14         foreach ($data["rows"] as $k => $model)
15         {
16             $team[] = $this->teamScoreService($model->id);
17         }
18 
19         $teamSort = array();
20         foreach ($team as $k => $v)
21         {
22             $teamSort[] = $v["total"];
23         }
24 
25 
26         //取出所有的学生:
27         $StudentModel = new StudentModel();
28         $where["where"] = " 1 =1 order by orderBy asc, id asc ";
29         $data = $StudentModel->getDataList($where);
30 
31         foreach ($data["rows"] as $k => $model)
32         {
33             $student[] = $this->studentScoreService($model->id);
34         }
35 
36         $studentSort = array();
37         foreach ($student as $k => $v)
38         {
39             $studentSort[] = $v["total"];
40         }
41 
42         //排序之前:
43         echo "<pre>";
44         echo "排序之前:";
45         var_dump($team);
46         var_dump($teamSort);
47         var_dump($student);
48         var_dump($studentSort);
49 
50         //排序:
51         array_multisort($teamSort,SORT_DESC,SORT_NUMERIC,$team);
52         array_multisort($studentSort,SORT_DESC,SORT_NUMERIC,$student);
53 
54         echo "排序之后:";
55         var_dump($team);
56         var_dump($student);
57 
58         $parameter = array();
59         $this->display("Home/iframe/zj.php", $parameter);
60     }

 

注意:排序数组

$teamSort[] = $v["total"]; 
对于是关联的数组,也可以是用
$teamsort[$k] = $v["total"]

参见:参考二:



 


 

 

 

 

 

排序之前: $team, $teamSort ,  $student , $studentSort

  1 排序之前:array(4) {
  2   [0]=>
  3   array(6) {
  4     ["kq"]=>
  5     int(20)
  6     ["rw"]=>
  7     float(20)
  8     ["zz"]=>
  9     int(40)
 10     ["ls"]=>
 11     int(18)
 12     ["total"]=>
 13     float(98)
 14     ["name"]=>
 15     string(4) "1组"
 16   }
 17   [1]=>
 18   array(6) {
 19     ["kq"]=>
 20     int(0)
 21     ["rw"]=>
 22     int(0)
 23     ["zz"]=>
 24     int(10)
 25     ["ls"]=>
 26     int(18)
 27     ["total"]=>
 28     int(28)
 29     ["name"]=>
 30     string(4) "2组"
 31   }
 32   [2]=>
 33   array(6) {
 34     ["kq"]=>
 35     int(0)
 36     ["rw"]=>
 37     int(0)
 38     ["zz"]=>
 39     int(15)
 40     ["ls"]=>
 41     int(18)
 42     ["total"]=>
 43     int(33)
 44     ["name"]=>
 45     string(4) "3组"
 46   }
 47   [3]=>
 48   array(6) {
 49     ["kq"]=>
 50     int(0)
 51     ["rw"]=>
 52     int(0)
 53     ["zz"]=>
 54     int(20)
 55     ["ls"]=>
 56     int(18)
 57     ["total"]=>
 58     int(38)
 59     ["name"]=>
 60     string(4) "4组"
 61   }
 62 }
 63 array(4) {
 64   [0]=>
 65   float(98)
 66   [1]=>
 67   int(28)
 68   [2]=>
 69   int(33)
 70   [3]=>
 71   int(38)
 72 }
 73 array(12) {
 74   [0]=>
 75   array(6) {
 76     ["kq"]=>
 77     int(20)
 78     ["rw"]=>
 79     float(20)
 80     ["zz"]=>
 81     int(10)
 82     ["ls"]=>
 83     int(6)
 84     ["total"]=>
 85     float(56)
 86     ["name"]=>
 87     string(6) "杨曼"
 88   }
 89   [1]=>
 90   array(6) {
 91     ["kq"]=>
 92     int(0)
 93     ["rw"]=>
 94     int(0)
 95     ["zz"]=>
 96     int(0)
 97     ["ls"]=>
 98     int(6)
 99     ["total"]=>
100     int(6)
101     ["name"]=>
102     string(9) "朱延月"
103   }
104   [2]=>
105   array(6) {
106     ["kq"]=>
107     int(0)
108     ["rw"]=>
109     int(0)
110     ["zz"]=>
111     int(0)
112     ["ls"]=>
113     int(6)
114     ["total"]=>
115     int(6)
116     ["name"]=>
117     string(9) "牛清倩"
118   }
119   [3]=>
120   array(6) {
121     ["kq"]=>
122     int(0)
123     ["rw"]=>
124     int(0)
125     ["zz"]=>
126     int(0)
127     ["ls"]=>
128     int(6)
129     ["total"]=>
130     int(6)
131     ["name"]=>
132     string(9) "施友康"
133   }
134   [4]=>
135   array(6) {
136     ["kq"]=>
137     int(0)
138     ["rw"]=>
139     int(0)
140     ["zz"]=>
141     int(10)
142     ["ls"]=>
143     int(6)
144     ["total"]=>
145     int(16)
146     ["name"]=>
147     string(9) "范俊召"
148   }
149   [5]=>
150   array(6) {
151     ["kq"]=>
152     int(0)
153     ["rw"]=>
154     int(0)
155     ["zz"]=>
156     int(0)
157     ["ls"]=>
158     int(6)
159     ["total"]=>
160     int(6)
161     ["name"]=>
162     string(9) "陈开娜"
163   }
164   [6]=>
165   array(6) {
166     ["kq"]=>
167     int(0)
168     ["rw"]=>
169     int(0)
170     ["zz"]=>
171     int(15)
172     ["ls"]=>
173     int(6)
174     ["total"]=>
175     int(21)
176     ["name"]=>
177     string(9) "杨梦佳"
178   }
179   [7]=>
180   array(6) {
181     ["kq"]=>
182     int(0)
183     ["rw"]=>
184     int(0)
185     ["zz"]=>
186     int(10)
187     ["ls"]=>
188     int(6)
189     ["total"]=>
190     int(16)
191     ["name"]=>
192     string(6) "丁宁"
193   }
194   [8]=>
195   array(6) {
196     ["kq"]=>
197     int(0)
198     ["rw"]=>
199     int(0)
200     ["zz"]=>
201     int(20)
202     ["ls"]=>
203     int(6)
204     ["total"]=>
205     int(26)
206     ["name"]=>
207     string(6) "脱瑞"
208   }
209   [9]=>
210   array(6) {
211     ["kq"]=>
212     int(0)
213     ["rw"]=>
214     int(0)
215     ["zz"]=>
216     int(0)
217     ["ls"]=>
218     int(6)
219     ["total"]=>
220     int(6)
221     ["name"]=>
222     string(6) "沈昕"
223   }
224   [10]=>
225   array(6) {
226     ["kq"]=>
227     int(0)
228     ["rw"]=>
229     int(0)
230     ["zz"]=>
231     int(0)
232     ["ls"]=>
233     int(6)
234     ["total"]=>
235     int(6)
236     ["name"]=>
237     string(9) "沈春香"
238   }
239   [11]=>
240   array(6) {
241     ["kq"]=>
242     int(0)
243     ["rw"]=>
244     int(0)
245     ["zz"]=>
246     int(20)
247     ["ls"]=>
248     int(6)
249     ["total"]=>
250     int(26)
251     ["name"]=>
252     string(9) "黄佳兴"
253   }
254 }
255 array(12) {
256   [0]=>
257   float(56)
258   [1]=>
259   int(6)
260   [2]=>
261   int(6)
262   [3]=>
263   int(6)
264   [4]=>
265   int(16)
266   [5]=>
267   int(6)
268   [6]=>
269   int(21)
270   [7]=>
271   int(16)
272   [8]=>
273   int(26)
274   [9]=>
275   int(6)
276   [10]=>
277   int(6)
278   [11]=>
279   int(26)
280 }

 

 

排序之后: $team,  $student

排序之后:array(4) {
  [0]=>
  array(6) {
    ["kq"]=>
    int(20)
    ["rw"]=>
    float(20)
    ["zz"]=>
    int(40)
    ["ls"]=>
    int(18)
    ["total"]=>
    float(98)
    ["name"]=>
    string(4) "1组"
  }
  [1]=>
  array(6) {
    ["kq"]=>
    int(0)
    ["rw"]=>
    int(0)
    ["zz"]=>
    int(20)
    ["ls"]=>
    int(18)
    ["total"]=>
    int(38)
    ["name"]=>
    string(4) "4组"
  }
  [2]=>
  array(6) {
    ["kq"]=>
    int(0)
    ["rw"]=>
    int(0)
    ["zz"]=>
    int(15)
    ["ls"]=>
    int(18)
    ["total"]=>
    int(33)
    ["name"]=>
    string(4) "3组"
  }
  [3]=>
  array(6) {
    ["kq"]=>
    int(0)
    ["rw"]=>
    int(0)
    ["zz"]=>
    int(10)
    ["ls"]=>
    int(18)
    ["total"]=>
    int(28)
    ["name"]=>
    string(4) "2组"
  }
}
array(12) {
  [0]=>
  array(6) {
    ["kq"]=>
    int(20)
    ["rw"]=>
    float(20)
    ["zz"]=>
    int(10)
    ["ls"]=>
    int(6)
    ["total"]=>
    float(56)
    ["name"]=>
    string(6) "杨曼"
  }
  [1]=>
  array(6) {
    ["kq"]=>
    int(0)
    ["rw"]=>
    int(0)
    ["zz"]=>
    int(20)
    ["ls"]=>
    int(6)
    ["total"]=>
    int(26)
    ["name"]=>
    string(6) "脱瑞"
  }
  [2]=>
  array(6) {
    ["kq"]=>
    int(0)
    ["rw"]=>
    int(0)
    ["zz"]=>
    int(20)
    ["ls"]=>
    int(6)
    ["total"]=>
    int(26)
    ["name"]=>
    string(9) "黄佳兴"
  }
  [3]=>
  array(6) {
    ["kq"]=>
    int(0)
    ["rw"]=>
    int(0)
    ["zz"]=>
    int(15)
    ["ls"]=>
    int(6)
    ["total"]=>
    int(21)
    ["name"]=>
    string(9) "杨梦佳"
  }
  [4]=>
  array(6) {
    ["kq"]=>
    int(0)
    ["rw"]=>
    int(0)
    ["zz"]=>
    int(10)
    ["ls"]=>
    int(6)
    ["total"]=>
    int(16)
    ["name"]=>
    string(6) "丁宁"
  }
  [5]=>
  array(6) {
    ["kq"]=>
    int(0)
    ["rw"]=>
    int(0)
    ["zz"]=>
    int(10)
    ["ls"]=>
    int(6)
    ["total"]=>
    int(16)
    ["name"]=>
    string(9) "范俊召"
  }
  [6]=>
  array(6) {
    ["kq"]=>
    int(0)
    ["rw"]=>
    int(0)
    ["zz"]=>
    int(0)
    ["ls"]=>
    int(6)
    ["total"]=>
    int(6)
    ["name"]=>
    string(9) "施友康"
  }
  [7]=>
  array(6) {
    ["kq"]=>
    int(0)
    ["rw"]=>
    int(0)
    ["zz"]=>
    int(0)
    ["ls"]=>
    int(6)
    ["total"]=>
    int(6)
    ["name"]=>
    string(9) "朱延月"
  }
  [8]=>
  array(6) {
    ["kq"]=>
    int(0)
    ["rw"]=>
    int(0)
    ["zz"]=>
    int(0)
    ["ls"]=>
    int(6)
    ["total"]=>
    int(6)
    ["name"]=>
    string(6) "沈昕"
  }
  [9]=>
  array(6) {
    ["kq"]=>
    int(0)
    ["rw"]=>
    int(0)
    ["zz"]=>
    int(0)
    ["ls"]=>
    int(6)
    ["total"]=>
    int(6)
    ["name"]=>
    string(9) "沈春香"
  }
  [10]=>
  array(6) {
    ["kq"]=>
    int(0)
    ["rw"]=>
    int(0)
    ["zz"]=>
    int(0)
    ["ls"]=>
    int(6)
    ["total"]=>
    int(6)
    ["name"]=>
    string(9) "牛清倩"
  }
  [11]=>
  array(6) {
    ["kq"]=>
    int(0)
    ["rw"]=>
    int(0)
    ["zz"]=>
    int(0)
    ["ls"]=>
    int(6)
    ["total"]=>
    int(6)
    ["name"]=>
    string(9) "陈开娜"
  }
}

 

以上是关于php 多维数组 array sort 排序 :array_multisort的主要内容,如果未能解决你的问题,请参考以下文章

php 多维数组指定字段排序

array_multisort — 对多个数组或多维数组进行排序

array_multisort — 对多个数组或多维数组进行排序

多维数组排序

实现对多维数组按照某个键值排序的两种方法(array_multisort和array_sort)

数组排序array_multisort