最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php 二维数组排序使用详解
时间:2022-06-24 22:45:12 编辑:袖梨 来源:一聚教程网
昨天要排序数组的时候发现了,要按时间排序,但是php并没有内设这个函数,所以在网上找到了这个代码,第一个参数为数组,第二个是要排序的元素,第三个为排序方式,
下面就是php 二维数组排序的代码
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) ;
array_multisort(array1,sorting order, sorting type,array2,array3..)是对多个数组或多维数组进行排序的函数。
function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){
if(is_array($arrays)){
foreach ($arrays as $array){
if(is_array($array)){
$key_arrays[] = $array[$sort_key];
}else{
return false;
}
}
}else{
return false;
}
array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
return $arrays;
}
$person = array(
array('id'=>1,'name'=>'fj','weight'=>100,'height'=>180),
array('id'=>2,'name'=>'tom','weight'=>53,'height'=>150),
array('id'=>3,'name'=>'jerry','weight'=>120,'height'=>156),
array('id'=>4,'name'=>'bill','weight'=>110,'height'=>190),
array('id'=>5,'name'=>'linken','weight'=>80,'height'=>200),
array('id'=>6,'name'=>'madana','weight'=>95,'height'=>110),
array('id'=>7,'name'=>'jordan','weight'=>70,'height'=>170)
);
var_dump($person);
$person = my_sort($person,'name',SORT_ASC,SORT_STRING);
var_dump($person);
$person = my_sort($person,'weight');
var_dump($person);
?>
结果如下:
array (size=7)
0 =>
array (size=4)
'id' => int 1
'name' => string 'fj' (length=2)
'weight' => int 100
'height' => int 180
1 =>
array (size=4)
'id' => int 2
'name' => string 'tom' (length=3)
'weight' => int 53
'height' => int 150
2 =>
array (size=4)
'id' => int 3
'name' => string 'jerry' (length=5)
'weight' => int 120
'height' => int 156
3 =>
array (size=4)
'id' => int 4
'name' => string 'bill' (length=4)
'weight' => int 110
'height' => int 190
4 =>
array (size=4)
'id' => int 5
'name' => string 'linken' (length=6)
'weight' => int 80
'height' => int 200
5 =>
array (size=4)
'id' => int 6
'name' => string 'madana' (length=6)
'weight' => int 95
'height' => int 110
6 =>
array (size=4)
'id' => int 7
'name' => string 'jordan' (length=6)
'weight' => int 70
'height' => int 170
array (size=7)
0 =>
array (size=4)
'id' => int 4
'name' => string 'bill' (length=4)
'weight' => int 110
'height' => int 190
1 =>
array (size=4)
'id' => int 1
'name' => string 'fj' (length=2)
'weight' => int 100
'height' => int 180
2 =>
array (size=4)
'id' => int 3
'name' => string 'jerry' (length=5)
'weight' => int 120
'height' => int 156
3 =>
array (size=4)
'id' => int 7
'name' => string 'jordan' (length=6)
'weight' => int 70
'height' => int 170
4 =>
array (size=4)
'id' => int 5
'name' => string 'linken' (length=6)
'weight' => int 80
'height' => int 200
5 =>
array (size=4)
'id' => int 6
'name' => string 'madana' (length=6)
'weight' => int 95
'height' => int 110
6 =>
array (size=4)
'id' => int 2
'name' => string 'tom' (length=3)
'weight' => int 53
'height' => int 150
array (size=7)
0 =>
array (size=4)
'id' => int 2
'name' => string 'tom' (length=3)
'weight' => int 53
'height' => int 150
1 =>
array (size=4)
'id' => int 7
'name' => string 'jordan' (length=6)
'weight' => int 70
'height' => int 170
2 =>
array (size=4)
'id' => int 5
'name' => string 'linken' (length=6)
'weight' => int 80
'height' => int 200
3 =>
array (size=4)
'id' => int 6
'name' => string 'madana' (length=6)
'weight' => int 95
'height' => int 110
4 =>
array (size=4)
'id' => int 1
'name' => string 'fj' (length=2)
'weight' => int 100
'height' => int 180
5 =>
array (size=4)
'id' => int 4
'name' => string 'bill' (length=4)
'weight' => int 110
'height' => int 190
6 =>
array (size=4)
'id' => int 3
'name' => string 'jerry' (length=5)
'weight' => int 120
'height' => int 156
这里的重点就是,先把要排序的key存到一个一维数组中,然后就可以使用array_multisort()这个函数,将数组按照key进行排序了,当然,这里的排序你完全可以不适用array_multisort()这个函数,仅仅通过foreach遍历也能达到这个效果,但是既然php开发者给我们提供了更好的办法,我们就可以省去不必要的麻烦了
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22