array_multisort()
函數能夠實現對多字段進行排序,相似對錶數據進行排序操做。
例如:php
地區 | 熱度 | 數量 |
---|---|---|
北京 | 32 | 1 |
上海 | 36 | 18 |
廣州 | 4 | 9 |
深圳 | 43 | 6 |
杭州 | 99 | 77 |
成都 | 99 | 80 |
昆明 | 4 | 6 |
重慶 | 99 | 60 |
代碼以下:函數
<?php $arr = [ ['num' => '1','heat' => 32,'name' => '北京'], ['num' => '18','heat' => 36,'name' => '上海'], ['num' => '9','heat' => 4,'name' => '廣州'], ['num' => '19','heat' => 36,'name' => '意義'], ['num' => '6','heat' => 43, 'name' => '深圳'], ['num' => '77','heat' => 99,'name' => '杭州'], ['num' => '78','heat' => 99,'name' => '成都'], ['num' => '6','heat' => 4,'name' => '昆明'], ['num' => '60','heat' => 99,'name' => '重慶'], ]; foreach ($arr as $key => $row) { $heat[$key] = $row['heat']; $num[$key] = $row['num']; } //將把 heat 降序排列,把 num 升序排列, 把 $arr 做爲最後一個參數,以通用鍵排序 array_multisort($heat, SORT_DESC, $num, SORT_ASC, $arr); print_r($arr);