PHP - sorting a 3-dimensional multidimensional array - sorting the base at the 3rd level, but sorting at the first level

What is the usual procedure for sorting a 3-dimensional multidimensional array base at the 3rd level value of "count". In this array, the number of samples can be 1,2,3,4,5 and so on. how to perform a transfer and set a larger data array of the count array, which will be sorted at the beginning using the index of the 1st level array.

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

(if 1 record contains the score 1, 2, 3, let it use the largest number of counters as a decisive variable for sorting)

An example of a 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 ) ) 
+4
source share
2 answers

This should work for sorting from low count to high count. Switch the last negative result to the last statement if you want it in another way.

 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; }); 
+2
source

If I understand correctly, you want to sort according to the total number of each record, therefore, in your example, record No. 175 has a total of 6 and should be the first in the list.

You can do this with usort , but it overwrites the keys of the array, 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 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); 
0
source

Source: https://habr.com/ru/post/1492995/


All Articles