我有一个看起来像
的数组Array
(
[0] => Array
(
[id] => 39662
[points] => 24
[subject] => 112
)
[1] => Array
(
[id] => 39609
[points] => 24
[subject] => 87
)
[2] => Array
(
[id] => 39610
[points] => 23
[subject] => 77
)
[3] => Array
(
[id] => 39608
[points] => 23
[subject] => 87
)
[4] => Array
(
[id] => 39606
[points] => 22
[subject] => 60
)
[5] => Array
(
[id] => 39604
[points] => 19
[subject] => 75
)
[6] => Array
(
[id] => 39595
[points] => 18
[subject] => 60
)
[7] => Array
(
[id] => 39605
[points] => 18
[subject] => 47
)
[8] => Array
(
[id] => 39650
[points] => 17
[subject] => 87
)
[9] => Array
(
[id] => 39660
[points] => 17
[subject] => 55
)
)
现在我想根据关键主题的数量进行排序。你可以看到subjuet = 87有3条记录而subject = 60有两条记录,所以87的所有三条记录应首先显示,然后是60条记录,然后是其他记录。
我尝试过array_multisort但它没有给出预期的结果。
由于
答案 0 :(得分:1)
根据您所需的输出,您只需要array_map()
array_multisort()
,
示例:强>
<?php
// Test Array
$array = array(
array('id'=>39662,'points'=>'24','subject'=>112),
array('id'=>39609,'points'=>'24','subject'=>87),
array('id'=>39610,'points'=>'23','subject'=>77),
array('id'=>39608,'points'=>'23','subject'=>87),
array('id'=>39606,'points'=>'22','subject'=>60),
array('id'=>39604,'points'=>'19','subject'=>75),
array('id'=>39595,'points'=>'18','subject'=>60),
array('id'=>39605,'points'=>'18','subject'=>47),
array('id'=>39650,'points'=>'17','subject'=>87),
array('id'=>39660,'points'=>'17','subject'=>55),
);
$newArr = array(); // initialize the new Array
foreach ($array as $key => $value)
{
$newArr[$value['subject']][] = $value;
}
array_multisort(array_map('count', $newArr), SORT_DESC, $newArr); // using array_multisort() and Sort as DESC order by using array_map()
echo "<pre>";
print_r($newArr);
?>
<强>结果:强>
Array
(
[0] => Array
(
[0] => Array
(
[id] => 39609
[points] => 24
[subject] => 87
)
[1] => Array
(
[id] => 39608
[points] => 23
[subject] => 87
)
[2] => Array
(
[id] => 39650
[points] => 17
[subject] => 87
)
)
[1] => Array
(
[0] => Array
(
[id] => 39606
[points] => 22
[subject] => 60
)
[1] => Array
(
[id] => 39595
[points] => 18
[subject] => 60
)
)
[2] => Array
(
[0] => Array
(
[id] => 39604
[points] => 19
[subject] => 75
)
)
[3] => Array
(
[0] => Array
(
[id] => 39605
[points] => 18
[subject] => 47
)
)
[4] => Array
(
[0] => Array
(
[id] => 39610
[points] => 23
[subject] => 77
)
)
[5] => Array
(
[0] => Array
(
[id] => 39660
[points] => 17
[subject] => 55
)
)
[6] => Array
(
[0] => Array
(
[id] => 39662
[points] => 24
[subject] => 112
)
)
)
答案 1 :(得分:0)
针对您的案例尝试以下方法
$data[] = array('points' => 67, 'subject' => 2);
$data[] = array('points' => 86, 'subject' => 1);
$data[] = array('points' => 85, 'subject' => 6);
$data[] = array('points' => 98, 'subject' => 2);
$data[] = array('points' => 86, 'subject' => 6);
$data[] = array('points' => 67, 'subject' => 7);
// Obtain a list of columns
foreach ($data as $key => $row) {
$subject[$key] = $row['subject'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($subject, SORT_DESC, $data);
使用php手册中的数组示例。
以上的输出是
Array
(
[0] => Array
(
[points] => 67
[subject] => 7
)
[1] => Array
(
[points] => 85
[subject] => 6
)
[2] => Array
(
[points] => 86
[subject] => 6
)
[3] => Array
(
[points] => 67
[subject] => 2
)
[4] => Array
(
[points] => 98
[subject] => 2
)
[5] => Array
(
[points] => 86
[subject] => 1
)
)
答案 2 :(得分:0)
function array_sort($array, $key){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $key) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
asort($sortable_array);
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
array_sort($a, 'subjects');
// $ a是要排序的数组