Php beginners ask a question. The original array structure is as follows. The original array is sorted by ads_id and time. The current requirement is to re-sort based on the sum of the num of the Two-dimensional array {code ...} the final implementation sorting is as follows: {code ...} ask a new php user a question;
The original array structure is as follows. The original array is sorted by ads_id and time;
Now we need to re-sort the data based on the num Sum of the Two-dimensional array.
Array( [5] => Array ( [0] => Array ( [num] => 840 [time] => 2015-10 [ads_id] => 5 ) [1] => Array ( [num] => 684 [time] => 2015-11 [ads_id] => 5 ) ) [6] => Array ( [0] => Array ( [num] => 23 [time] => 2015-10 [ads_id] => 6 ) [1] => Array ( [num] => 37 [time] => 2015-11 [ads_id] => 6 ) ) [7] => Array ( [0] => Array ( [num] => 280 [time] => 2015-10 [ads_id] => 7 ) [1] => Array ( [num] => 343 [time] => 2015-11 [ads_id] => 7 ) ))
The sorting is as follows:
Array( [5] => Array ( [0] => Array ( [num] => 840 [time] => 2015-10 [ads_id] => 5 ) [1] => Array ( [num] => 684 [time] => 2015-11 [ads_id] => 5 ) ) [7] => Array ( [0] => Array ( [num] => 280 [time] => 2015-10 [ads_id] => 7 ) [1] => Array ( [num] => 343 [time] => 2015-11 [ads_id] => 7 ) ) [6] => Array ( [0] => Array ( [num] => 23 [time] => 2015-10 [ads_id] => 6 ) [1] => Array ( [num] => 37 [time] => 2015-11 [ads_id] => 6 ) ))
Reply content:
Ask a new php user a question;
The original array structure is as follows. The original array is sorted by ads_id and time;
Now we need to re-sort the data based on the num Sum of the Two-dimensional array.
Array( [5] => Array ( [0] => Array ( [num] => 840 [time] => 2015-10 [ads_id] => 5 ) [1] => Array ( [num] => 684 [time] => 2015-11 [ads_id] => 5 ) ) [6] => Array ( [0] => Array ( [num] => 23 [time] => 2015-10 [ads_id] => 6 ) [1] => Array ( [num] => 37 [time] => 2015-11 [ads_id] => 6 ) ) [7] => Array ( [0] => Array ( [num] => 280 [time] => 2015-10 [ads_id] => 7 ) [1] => Array ( [num] => 343 [time] => 2015-11 [ads_id] => 7 ) ))
The sorting is as follows:
Array( [5] => Array ( [0] => Array ( [num] => 840 [time] => 2015-10 [ads_id] => 5 ) [1] => Array ( [num] => 684 [time] => 2015-11 [ads_id] => 5 ) ) [7] => Array ( [0] => Array ( [num] => 280 [time] => 2015-10 [ads_id] => 7 ) [1] => Array ( [num] => 343 [time] => 2015-11 [ads_id] => 7 ) ) [6] => Array ( [0] => Array ( [num] => 23 [time] => 2015-10 [ads_id] => 6 ) [1] => Array ( [num] => 37 [time] => 2015-11 [ads_id] => 6 ) ))
function mySort($a, $b) { $sumA = 0; $sumB = 0; foreach($a as $ele) { $sumA += $ele['num']; } foreach($b as $ele) { $sumB += $ele['num']; } return $sumB < $sumA;}usort($arr, 'mySort');
Useusort
Custom sorting rules