PHP多维数组多字段排序分组,处理的数据比较多,望高手解决

php中文网
发布: 2016-06-23 13:16:03
原创
1057人浏览过


原数组

        $data = array(            0  => array(                'id'    => '510851539496070',                'name'  => 'a',                'title' => '108a',                'time'  => '1389230133',            ),            1  => array(                'id'    => '510851539496070',                'name'  => 'a',                'title' => '108a',                'time'  => '1389230129',            ),            2  => array(                'id'    => '510940988802058',                'name'  => 'a',                'title' => '109a',                'time'  => '1389241098',            ),            3  => array(                'id'    => '510868001733128',                'name'  => 'a',                'title' => '109a',                'time'  => '1389230169',            ),            4  => array(                'id'    => '510558107108314',                'name'  => 'a',                'title' => '132a',                'time'  => '1389191699',            ),            5  => array(                'id'    => '509763928740655',                'name'  => 'a',                'title' => '136a',                'time'  => '1389225520',            ),            6  => array(                'id'    => '509763928740655',                'name'  => 'a',                'title' => '143a',                'time'  => '1389225520',            ),            7  => array(                'id'    => '509763928740655',                'name'  => 'a',                'title' => '143a',                'time'  => '1389225520',            ),            8  => array(                'id'    => '510851539496070',                'name'  => 'a',                'title' => '245a',                'time'  => '1389230129',            ),            9  => array(                'id'    => '510558107108314',                'name'  => 'a',                'title' => '256a',                'time'  => '1389191699',            ),            10 => array(                'id'    => '510868001733128',                'name'  => 'a',                'title' => '262a',                'time'  => '1389230169',            ),            11 => array(                'id'    => '509763928740655',                'name'  => 'a',                'title' => '306a',                'time'  => '1389225521',            ),            12 => array(                'id'    => '509763928740655',                'name'  => 'a',                'title' => '306a',                'time'  => '1389225520',            ),            13 => array(                'id'    => '509763928740655',                'name'  => 'a',                'title' => '315a',                'time'  => '1389225522',            ),            14 => array(                'id'    => '510868001733128',                'name'  => 'a',                'title' => '403a',                'time'  => '1389230169',            ),            15 => array(                'id'    => '510558107108314',                'name'  => 'a',                'title' => '404a',                'time'  => '1389191699',            ),            16 => array(                'id'    => '510558107108314',                'name'  => 'a',                'title' => '404a',                'time'  => '1389191699',            ),            17 => array(                'id'    => '510558107108314',                'name'  => 'a',                'title' => '404a',                'time'  => '1389191699',            ),            18 => array(                'id'    => '510558107108314',                'name'  => 'a',                'title' => '404a',                'time'  => '1389191699',            ),            19 => array(                'id'    => '510558107108314',                'name'  => 'a',                'title' => '404a',                'time'  => '1389191699',            ),            20 => array(                'id'    => '510558107108314',                'name'  => 'b',                'title' => '40b',                'time'  => '1389191699',            ),        );
登录后复制


假设我们把id相同的看成一个组)要求id相同的分组在一起,每个分组之间按title、time再排序
最后输出:
        array(            0  => array(                'id'    => '510851539496070',                'name'  => 'a',                'title' => '108a',                'time'  => '1389230129',            ),            1  => array(                'id'    => '510851539496070',                'name'  => 'a',                'title' => '108a',                'time'  => '1389230133',            ),            2  => array(                'id'    => '510851539496070',                'name'  => 'a',                'title' => '245a',                'time'  => '1389230129',            ),            3  => array(                'id'    => '510868001733128',                'name'  => 'a',                'title' => '109a',                'time'  => '1389230169',            ),            4  => array(                'id'    => '510868001733128',                'name'  => 'a',                'title' => '262a',                'time'  => '1389230169',            ),            5  => array(                'id'    => '510868001733128',                'name'  => 'a',                'title' => '403a',                'time'  => '1389230169',            ),            6  => array(                'id'    => '510940988802058',                'name'  => 'a',                'title' => '109a',                'time'  => '1389241098',            ),            7  => array(                'id'    => '510558107108314',                'name'  => 'a',                'title' => '132a',                'time'  => '1389191699',            ),            8  => array(                'id'    => '510558107108314',                'name'  => 'a',                'title' => '256a',                'time'  => '1389191699',            ),            9  => array(                'id'    => '510558107108314',                'name'  => 'a',                'title' => '404a',                'time'  => '1389191699',            ),            10 => array(                'id'    => '510558107108314',                'name'  => 'a',                'title' => '404a',                'time'  => '1389191699',            ),            11 => array(                'id'    => '510558107108314',                'name'  => 'a',                'title' => '404a',                'time'  => '1389191699',            ),            12 => array(                'id'    => '510558107108314',                'name'  => 'a',                'title' => '404a',                'time'  => '1389191699',            ),            13 => array(                'id'    => '510558107108314',                'name'  => 'a',                'title' => '404a',                'time'  => '1389191699',            ),            14 => array(                'id'    => '510558107108314',                'name'  => 'b',                'title' => '40b',                'time'  => '1389191699',            ),            15 => array(                'id'    => '509763928740655',                'name'  => 'a',                'title' => '136a',                'time'  => '1389225520',            ),            16 => array(                'id'    => '509763928740655',                'name'  => 'a',                'title' => '143a',                'time'  => '1389225520',            ),            17 => array(                'id'    => '509763928740655',                'name'  => 'a',                'title' => '143a',                'time'  => '1389225520',            ),            18 => array(                'id'    => '509763928740655',                'name'  => 'a',                'title' => '306a',                'time'  => '1389225520',            ),            19 => array(                'id'    => '509763928740655',                'name'  => 'a',                'title' => '306a',                'time'  => '1389225521',            ),            20 => array(                'id'    => '509763928740655',                'name'  => 'a',                'title' => '315a',                'time'  => '1389225522',            ),        );
登录后复制



我的做法如下,数据比较多时效率不高! 请高手提供更好的方法:
        //先排序title,time        foreach ($data as $key => $row) {            $title[$key] = $row['title'];            $time[$key]  = $row['time'];        }        array_multisort($title, sort_asc, sort_regular, $time, sort_asc, sort_regular, $data);        //相同id临时放到一起        $tem = array();        foreach ($data as $value) {            $tem[$value['id']][] = $value;        }        //输出最终结果        $result = array();        foreach ($tem as $sub_arr) {            foreach ($sub_arr as $value) {                $result[] = $value;            }        }        print_r($result);
登录后复制

喵记多
喵记多

喵记多 - 自带助理的 AI 笔记

喵记多 27
查看详情 喵记多


回复讨论(解决方案)

这样不行吗

foreach($data as $r) {  $id[] = $r['id'];  $title[] = $r['title'];  $time[] = $r['time'];}array_multisort($id, $title, $time, $data);print_r($data);
登录后复制

这样不行吗

foreach($data as $r) {  $id[] = $r['id'];  $title[] = $r['title'];  $time[] = $r['time'];}array_multisort($id, $title, $time, $data);print_r($data);
登录后复制



这种得不到结果。主要排序是title,time(只是需要将id相同的放在一起)

//输出最终结果
$result = call_user_func_array('array_merge', $tem);

我来学习一下

土豪村长兄么

暂时想法和你一样

http://www.9958.pw/post/php_array_multi_sort

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号