请教一个php多维数组排序问题

WBOY
Lepaskan: 2016-06-06 20:15:20
asal
912 orang telah melayarinya

php新手请教一个问题;
原数组结构如下,原数组是按ads_id,time进行排序;
现在的需求是根据二维数组的num总和重新进行排序

<code>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
                )
        )
)
</code>
Salin selepas log masuk
Salin selepas log masuk

最终实现排序如下:

<code>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
                )
        )
)
</code>
Salin selepas log masuk
Salin selepas log masuk

回复内容:

php新手请教一个问题;
原数组结构如下,原数组是按ads_id,time进行排序;
现在的需求是根据二维数组的num总和重新进行排序

<code>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
                )
        )
)
</code>
Salin selepas log masuk
Salin selepas log masuk

最终实现排序如下:

<code>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
                )
        )
)
</code>
Salin selepas log masuk
Salin selepas log masuk

<code>function mySort($a, $b) {
    $sumA = 0;
    $sumB = 0;
    foreach($a as $ele) {
        $sumA += $ele['num'];
    }
    foreach($b as $ele) {
        $sumB += $ele['num'];
    }
    return $sumB </code>
Salin selepas log masuk

使用 usort 自定义排序规则

Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan