昨天要排序数组的时候发现了,要按时间排序,但是php并没有内设这个函数,所以在网上找到了这个代码,第一个参数为数组,第二个是要排序的元素,第三个为排序方式,
下面就是php 二维数组排序的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
function arraySort( $arr , $keys , $type = 'asc' ) { $keysvalue = $new_array = array (); foreach ( $arr as $k => $v ){ $keysvalue [ $k ] = $v [ $keys ]; } $type == 'asc' ? asort( $keysvalue ) : arsort( $keysvalue ); reset( $keysvalue ); foreach ( $keysvalue as $k => $v ) { $new_array [ $k ] = $arr [ $k ]; } return $new_array ; } $arr [] = array ( "name" => "1" , "time" =>1) ; $arr [] = array ( "name" => "2" , "time" =>2); arraySort( $arr , "time" , "desc" ); |