PHP - 对3级多维数组进行排序 - 基于第3级值排序,但在第一级排序

时间:2013-07-23 15:37:43

标签: php arrays sorting multidimensional-array

基于第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
            )

    )

2 个答案:

答案 0 :(得分:2)

这适用于从低计数到高计数的排序。如果您想要反过来,请在最后一个语句中切换最后一个结尾。

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;

});

答案 1 :(得分:0)

如果我理解正确,您希望根据每条记录的总计数进行排序,因此在您的示例中,记录#175的总计数为6,应该在列表中排在第一位。

您可以使用usort执行此操作,但它会覆盖数组键,因此我使用了multisort并对键和值进行了排序:

$counts = array();
$keys = array_keys($arr);
foreach ($arr as $key => $value){
    $total = 0;
    foreach ($value as $key2 => $value2){
        $total -= $value2['count'];
    }
    $counts[] = $total;//it's actually negative totals to sort in descending order
}
$counts_copy = $counts;//array_multisort will rearrange, so we need a copy
array_multisort($counts, $arr);
array_multisort($counts_copy, $keys);
$out = array_combine($keys, $arr);
print_r($out);