TypechoJoeTheme

Dcr163的博客

统计

php使用usort排序多维数组内的值

2025-09-11
/
0 评论
/
9 阅读
/
正在检测是否收录...
09/11

直接上代码:

$data = [
    '一队' => [
        'counts' => 0,
        'totals' => 0,
        'team'   => [
            ['realname' => '张三', 'counts' => 10, 'totals' => 600],
            ['realname' => '张三2', 'counts' => 17, 'totals' => 478],
            ['realname' => '张三3', 'counts' => 190, 'totals' => 10000],
            ['realname' => '张三4', 'counts' => 8, 'totals' => 500],
        ]
    ],
    '二队' => [
        'counts' => 0,
        'totals' => 0,
        'team'   => [
            ['realname' => '李四', 'counts' => 10, 'totals' => 600],
            ['realname' => '李四2', 'counts' => 17, 'totals' => 478],
            ['realname' => '李四3', 'counts' => 190, 'totals' => 10000],
            ['realname' => '李四4', 'counts' => 8, 'totals' => 500],
        ]
    ],
    '三队' => [
        'counts' => 0,
        'totals' => 0,
        'team'   => [
            ['realname' => '王五', 'counts' => 10, 'totals' => 600],
            ['realname' => '王五2', 'counts' => 17, 'totals' => 478],
            ['realname' => '王五3', 'counts' => 190, 'totals' => 10000],
            ['realname' => '王五4', 'counts' => 8, 'totals' => 500],
            ['realname' => '王五5', 'counts' => 11, 'totals' => 600],
        ]
    ],
];
//数组根据指定key排序
foreach ($data as $key => &$val) {
    usort($val['team'], function ($a, $b) {
        //根据totals从高到低排序
        if ($a['totals'] != $b['totals']) {
            if ($a['totals'] > $b['totals']) {  // 如果是 < 就是从低到高排序
                return -1;
            } elseif ($a['totals'] == $b['totals']) {
                return 0;
            } else {
                return 1;
            }
            //php7+可以这么写,使用航天飞船符 <=> 更简洁,逻辑清晰
            //return $b['totals'] <=> $a['totals'];
        }

        //根据counts从高到底排序
        if ($a['counts'] > $b['counts']) {  // 如果是 < 就是从低到高排序
            return -1;
        } elseif ($a['counts'] == $b['counts']) {
            return 0;
        } else {
            return 1;
        }
        //php7+可以这么写,使用航天飞船符 <=> 更简洁,逻辑清晰
        //return $b['counts'] <=> $a['counts'];
    });
}
unset($val);
echo '<pre>';
print_r($data);

看看最终输出的结果:

Array
(
    [一队] => Array
        (
            [counts] => 0
            [totals] => 0
            [team] => Array
                (
                    [0] => Array
                        (
                            [realname] => 张三3
                            [counts] => 190
                            [totals] => 10000
                        )

                    [1] => Array
                        (
                            [realname] => 张三
                            [counts] => 10
                            [totals] => 600
                        )

                    [2] => Array
                        (
                            [realname] => 张三4
                            [counts] => 8
                            [totals] => 500
                        )

                    [3] => Array
                        (
                            [realname] => 张三2
                            [counts] => 17
                            [totals] => 478
                        )

                )

        )

    [二队] => Array
        (
            [counts] => 0
            [totals] => 0
            [team] => Array
                (
                    [0] => Array
                        (
                            [realname] => 李四3
                            [counts] => 190
                            [totals] => 10000
                        )

                    [1] => Array
                        (
                            [realname] => 李四
                            [counts] => 10
                            [totals] => 600
                        )

                    [2] => Array
                        (
                            [realname] => 李四4
                            [counts] => 8
                            [totals] => 500
                        )

                    [3] => Array
                        (
                            [realname] => 李四2
                            [counts] => 17
                            [totals] => 478
                        )

                )

        )

    [三队] => Array
        (
            [counts] => 0
            [totals] => 0
            [team] => Array
                (
                    [0] => Array
                        (
                            [realname] => 王五3
                            [counts] => 190
                            [totals] => 10000
                        )

                    [1] => Array
                        (
                            [realname] => 王五5
                            [counts] => 11
                            [totals] => 600
                        )

                    [2] => Array
                        (
                            [realname] => 王五
                            [counts] => 10
                            [totals] => 600
                        )

                    [3] => Array
                        (
                            [realname] => 王五4
                            [counts] => 8
                            [totals] => 500
                        )

                    [4] => Array
                        (
                            [realname] => 王五2
                            [counts] => 17
                            [totals] => 478
                        )

                )

        )

)
PHPusort排序
朗读
赞(0)
版权属于:

Dcr163的博客

本文链接:

http://dcr163.cn/759.html(转载时请注明本文出处及文章链接)

评论 (0)

人生倒计时

今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月

最新回复

  1. http://goldenfiber.ru/forum/?PAGE_NAME=profile_view&UID=55151&backurl=%2Fforum%2F%3FPAGE_NAME%3Dprofile_view%26UID%3D32514
    2025-07-10
  2. https://www.triptipedia.com/user/phmapaladnet
    2025-05-08
  3. https://Sites.google.com/view/vavada-online-casino
    2025-04-12
  4. https://x.com/jeetbuzzcom
    2025-04-07
  5. prozone.cc alternative
    2025-03-19

标签云