Ask a php multi-dimensional array sorting question

Source: Internet
Author: User
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');

UseusortCustom sorting rules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.