PHP——对一个三级多维数组进行排序——根据第三级值进行排序,但在第一级进行排序


PHP - sorting a 3 level multidimensional array - sort base on 3rd level value but sort it in first level

根据三级数组值"count"对三级多维数组进行排序的正常过程是什么。在这个数组中,计数可以是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;
});

如果我理解正确,您希望根据每条记录的总数进行排序,因此在您的示例中,记录#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);