Ask a PHP multi-dimensional array sorting problem

Source: Internet
Author: User
PHP Novice to ask a question;
The original array structure is as follows, the original array is sorted by Ads_id,time;
The requirement now is to reorder the NUM sums 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] =&G T                 [TIME] = 2015-10 [ads_id] = 6) [1] = = Array (num] = Notoginseng [TIME] = 2015-11 [ads_id] =& Gt 6)) [7] = = Array ([0] = = Array ([nu M] = 280 [TIME] = 2015-10 [ads_id] = 7) [1] =       > Array         (num] = 343 [TIME] = 2015-11 [ads_id] + 7 )        ))

The

Final implementation is sorted 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] = [TIME] = 2015-10 [ads_id] = 6 ) [1] => Array ([num] = Notoginseng [TIME] = 2015-11 [ ADS_ID] = 6)))

Reply content:

PHP Novice to ask a question;
The original array structure is as follows, the original array is sorted by Ads_id,time;
The requirement now is to reorder the NUM sums 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] =&G T                 [TIME] = 2015-10 [ads_id] = 6) [1] = = Array (num] = Notoginseng [TIME] = 2015-11 [ads_id] =& Gt 6)) [7] = = Array ([0] = = Array ([nu M] = 280 [TIME] = 2015-10 [ads_id] = 7) [1] =       > Array         (num] = 343 [TIME] = 2015-11 [ads_id] + 7 )        ))

The

Final implementation is sorted 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] = [TIME] = 2015-10 [ads_id] = 6 ) [1] => Array ([num] = Notoginseng [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');

Using usort Custom Collations

  • 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.