我有一个数组,每个数组有4个值
$array[0] = array('amount' => '98.60', 'typeA' => '98.52', 'typeB' => '58.52', 'typeC' => '90.2');
$array[1] = array('amount' => '55.80', 'typeA' => '25.36', 'typeB' => '36.54', 'typeC' => '36.99');
$array[2] = array('amount' => '42.68', 'typeA' => '64.26', 'typeB' => '65.87', 'typeC' => '99.24');
$array[3] = array('amount' => '812.3', 'typeA' => '36.27', 'typeB' => '23.25', 'typeC' => '94.35');
我需要根据每个键的最高值排列数组,序列为:
所以最后我会看到哪个数量最多。
希望能在这里给予一些帮助,谢谢!
答案 0 :(得分:3)
$ data = array(array(' amount' =>' 98.60',' typeA' =>' 98.52',' typeB' =>' 58.52',' typeC' =>' 90.2'),数组('金额' =>' 55.80',' typeA' =>' 25.36',' typeB&#39 ; =>' 36.54',' typeC' =>' 36.99'),数组('金额' =>& #39; 42.68',' typeA' =>' 64.26',' typeB' =>' 65.87' ,' typeC' =>' 99.24'),数组('数量' =>' 812.3',' typeA' =>' 36.27',' typeB' =>' 23.25',' typeC' =>& #39; 94.35'));
//获取列列表
foreach($ data as $ key => $ row){
$volume[$key] = $row['typeA'];
$edition[$key] = $row['typeB'];
$edition1[$key] = $row['typeC'];
}
array_multisort($ volume,SORT_DESC,$ edition,SORT_DESC,$ edition1,SORT_DESC,$ data);
回声'&#39 ;;的print_r($数据);答案 1 :(得分:1)
array_multisort()可用于一次对多个数组进行排序,或者按一个或多个维度对多维数组进行排序。
http://php.net/manual/en/function.array-multisort.php
<?php
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);
// Pass the array, followed by the column names and sort flags
$sorted = array_orderby($data, 'volume', SORT_DESC, 'edition', SORT_ASC);
?>
这是另一个很好用的例子: