php的array_multisort()使用

WBOY
Release: 2016-06-23 14:30:23
Original
1010 people have browsed it

这段时间做数据统计,产品那边要求数据能按高到低排序,想了很多方法,都不行。最后找到PHP的array_multisort()方法。虽然实现了效果,但对这个方法还是半董半董

下边就是使用的实例,手册上的例子不列了,就我自己工作中的几个。

工作中碰到最多的可能就是二维数组了,想试试三维数组,但是想想还是算了。

 1 header('Content-Type: text/html; charset=utf-8'); 2 echo '<pre class="brush:php;toolbar:false">'; 3 //原始数组格式 4 $array = array( 5     'key1' => array( 6         'item1' => '65', 7         'item2' => '35', 8         'item3' => '84', 9     ),10     'key2' => array(11         'item1' => '24',12     ),13     'key3' => array(14         'item1' => '38',15         'item3' => '45',16     ),17 );18 //要排序的键19 //按照数组中的 item1进行排序20 //你也可以换成item221 $sort = 'item1';22 foreach($array as $k => $v)23 {24     $newArr[$k] = $v[$sort];25 }26 //这个函数如果执行正确他会直接改变原数组键值的顺序27 //如果执行失败,那么他会返回 bool(false)28 array_multisort($newArr,SORT_DESC, $array);29 var_dump($array);30 //---------------------排序后的数组打印效果 开始--------------------31 array(3) {32   ["key1"]=>33   array(3) {34     ["item1"]=>35     string(2) "65"36     ["item2"]=>37     string(2) "35"38     ["item3"]=>39     string(2) "84"40   }41   ["key3"]=>42   array(2) {43     ["item1"]=>44     string(2) "38"45     ["item3"]=>46     string(2) "45"47   }48   ["key2"]=>49   array(1) {50     ["item1"]=>51     string(2) "24"52   }53 }54 //---------------------排序后的数组打印效果 结束---------------------
Copy after login

 

Related labels:
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