php多维数组对成员内数据重排序uasort或array_multisort
php多维数组对成员内数据重排序uasort或array_multisort
一维数据
一维数据<?php
$fruits = array('Orange9','Orange11','Orange10','Orange6','Orange15');
uasort ( $fruits , function ($a, $b) {
return strnatcmp($a,$b); // or other function/code
}
);
print_r($fruits);
?>
一维数据<?php
//Comparisonfunction
functioncmp($a,$b){
if($a==$b){
return0;
}
return($a<$b)?-1:1;
}
//Arraytobesorted
$array=array('a'=>4,'b'=>8,'c'=>-1,'d'=>-9,'e'=>2,'f'=>5,'g'=>3,'h'=>-4);
print_r($array);
//Sortandprinttheresultingarray
uasort($array,'cmp');
print_r($array);
?>
多维数据<?php
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);
var_dump($ar1);
var_dump($ar2);
?>
多维数据,第一顺序,第二顺序排序法<?php
$ar = array(
array("10", 11, 100, 100, "a"),
array( 1, 2, "2", 3, 1)
);
array_multisort($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>
<?php
$data = [
0 => ['volume' => 67, 'edition' => 2],
1 => ['volume' => 67, 'edition' => 2],
2 => ['volume' => 86, 'edition' => 1],
3 => ['volume' => 85, 'edition' => 6],
4 => ['volume' => 98, 'edition' => 2],
5 => ['volume' => 86, 'edition' => 6],
6 => ['volume' => 67, 'edition' => 7]
];
// 取得列的列表
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}
// 将数据根据 volume 降序排列,根据 edition 升序排列
// 把 $data 作为最后一个参数,以通用键排序
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>