4

根据 3 级数组值“count”对 3 级多维数组进行排序的正常过程是什么。在这个数组中,计数可能是 1,2,3,4,5 等等。如何执行将较大计数的数组数据集按一级数组索引排序到开头。

基本上,如果数组记录在“计数”中具有较大的数字,则使其按降序排列在数组的开头。

(如果 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
            )

    )
4

2 回答 2

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;

});
于 2013-07-23T17:24:50.447 回答
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);
于 2013-07-24T16:36:18.647 回答