首页 > 后端开发 > php教程 > 请教一个php多维数组排序问题

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

WBOY
发布: 2016-06-06 20:15:20
原创
926 人浏览过

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>
登录后复制
登录后复制

最终实现排序如下:

<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>
登录后复制
登录后复制

回复内容:

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>
登录后复制
登录后复制

最终实现排序如下:

<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>
登录后复制
登录后复制

<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 < $sumA;
}

usort($arr, 'mySort');</code>
登录后复制

使用 usort 自定义排序规则

相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板