4

What is the normal procedure to sort a 3 level multidimensional array base on the 3rd level array value "count". In this array, the count number may be 1,2,3,4,5 and so on. how to carryout and have the larger count number array data set to be sorted into the beginning by the 1st level array index.

basically if an array record has a larger number in 'count' let it be in the beginning of the array in a descending order.

(if 1 record contains count 1, 2, 3, let it use the largest count number as the decision variable for the sorting)

sample multidimensional array looks like

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
            )

    )
1
  • Use usort() with a comparison function that extracts the values to compare from the 2nd level of the arguments. Commented Jul 23, 2013 at 15:41

2 Answers 2

2

This should work for sorting from low count to high count. Switch the last negative on the last statement if you want it the other way around.

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;

});
Sign up to request clarification or add additional context in comments.

Comments

0

If I understood correctly, you want to sort according to the total count of each record, so in your example, record #175 has a total count of 6 and should be first in the list.

You can do this with usort, but it overwrites the array keys, so instead I used multisort and sorted both the keys and the values:

$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.