在第3级数组值“count”上对3级多维数组进行排序的常规过程是什么?在此数组中,计数可以是1,2,3,4,5,依此类推.如何进行输出并将较大的计数数组数据集按第一级数组索引排序到开头.
基本上,如果一个数组记录在’count’中有一个更大的数字,那么让它以降序排列在数组的开头.
(如果1条记录包含计数1,2,3,则让它使用最大计数作为排序的决策变量)
示例多维数组看起来像
Array
(
[174] => Array
(
[28] => Array
(
[index] => 28
[draw] => 1
[date] => 12-05-2036
[value_a_key] => 7
[value_b_key] => 2
[value_c_key] => 4
[value_a] => 1
[value_b] => 5
[value_c] => 12
[count] => 1
)
)
[175] => Array
(
[19] => Array
(
[index] => 19
[draw] => 10
[date] => 12-05-2027
[value_a_key] => 2
[value_b_key] => 4
[value_c_key] => 3
[value_a] => 1
[value_b] => 5
[value_c] => 13
[count] => 1
)
[26] => Array
(
[index] => 26
[draw] => 3
[date] => 12-05-2034
[value_a_key] => 5
[value_b_key] => 4
[value_c_key] => 2
[value_a] => 1
[value_b] => 5
[value_c] => 13
[count] => 2
)
[28] => Array
(
[index] => 28
[draw] => 1
[date] => 12-05-2036
[value_a_key] => 7
[value_b_key] => 2
[value_c_key] => 5
[value_a] => 1
[value_b] => 5
[value_c] => 13
[count] => 3
)
)
[178] => Array
(
[19] => Array
(
[index] => 19
[draw] => 10
[date] => 12-05-2027
[value_a_key] => 2
[value_b_key] => 4
[value_c_key] => 7
[value_a] => 1
[value_b] => 5
[value_c] => 16
[count] => 1
)
)
最佳答案 这应该适用于从低计数到高计数的排序.如果您想要反过来,请在最后一个语句中切换最后一个结尾.
usort($array, function ($x, $y) {
// Set highest count to 0 for both arrays
$highestCountForX = 0;
$highestCountForY = 0;
// Loop through first array to check
foreach ($x as $secondLevelX) {
if ($secondLevelX['count'] > $highestCountForX) {
$highestCountForX = $secondLevelX['count'];
}
}
// Loop through second array to check
foreach ($y as $secondLevelY) {
if ($secondLevelY['count'] > $highestCountForY) {
$highestCountForY = $secondLevelY['count'];
}
}
if ($highestCountForX === $highestCountForY) {
return 0;
}
return ($highestCountForX < $highestCountForY) ? -1 : 1;
});