PHP多维数组排序(array_multisort)

  • Post author:
  • Post category:php


$result[0]['nome']='Joao';
$result[0]['order']=5;
$result[1]['nome']='Pedro';
$result[1]['order']=1;
$result[2]['nome']='Marcelo';
$result[2]['order']=3;

foreach($result as $res){
	$sortAux[] = $res['order'];
}
//第一个参数指定排序的列,$result为行
array_multisort($sortAux, SORT_ASC, $result);

print_r($result); echo "<hr>";
//Array ( [0] => Array ( [nome] => Pedro [order] => 1 ) [1] => Array ( [nome] => Marcelo [order] => 3 ) [2] => Array ( [nome] => Joao [order] => 5 ) ) 

$result = array(
	'Joao' => array(1,5),
	'Pedro' => array(2,1),
	'Marcelo' => array(3,3),
);
$sortAux = array(5,1,3); 

array_multisort($sortAux, SORT_ASC, $result);

print_r($result); echo "<hr>";
//Array ( [Pedro] => Array ( [0] => 2 [1] => 1 ) [Marcelo] => Array ( [0] => 3 [1] => 3 ) [Joao] => Array ( [0] => 1 [1] => 5 ) )