Home > php教程 > php手册 > php的array_multisort()使用方法介绍_php入门_脚本之家

php的array_multisort()使用方法介绍_php入门_脚本之家

WBOY
Release: 2016-06-06 20:40:43
Original
1214 people have browsed it

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

下边就是使用的实例,手册上的例子不列了,就我自己工作中的几个。
工作中碰到最多的可能就是二维数组了,想试试三维数组,但是想想还是算了。
代码如下:
header('Content-Type: text/html; charset=utf-8');
echo '
'; <br>//原始数组格式 <br>$array = array( <br>'key1' => array( <br>'item1' => '65', <br>'item2' => '35', <br>'item3' => '84', <br>), <br>'key2' => array( <br>'item1' => '24', <br>), <br>'key3' => array( <br>'item1' => '38', <br>'item3' => '45', <br>), <br>); <br>//要排序的键 <br>//按照数组中的 item1进行排序 <br>//你也可以换成item2 <br>$sort = 'item1'; <br>foreach($array as $k => $v) <br>{ <br>$newArr[$k] = $v[$sort]; <br>} <br>//这个函数如果执行正确他会直接改变原数组键值的顺序 <br>//如果执行失败,那么他会返回 bool(false) <br>array_multisort($newArr,SORT_DESC, $array); <br>var_dump($array); <br>//---------------------排序后的数组打印效果 开始-------------------- <br>array(3) { <br>["key1"]=> <br>array(3) { <br>["item1"]=> <br>string(2) "65" <br>["item2"]=> <br>string(2) "35" <br>["item3"]=> <br>string(2) "84" <br>} <br>["key3"]=> <br>array(2) { <br>["item1"]=> <br>string(2) "38" <br>["item3"]=> <br>string(2) "45" <br>} <br>["key2"]=> <br>array(1) { <br>["item1"]=> <br>string(2) "24" <br>} <br>} <br>//---------------------排序后的数组打印效果 结束--------------------- <br> <br>


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template