排序多维数组由多个键 [英] Sort multidimensional array by multiple keys

查看:117
本文介绍了排序多维数组由多个键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想排序多个键多维数组,我不知道从哪里开始。我看着uasort,但不太清楚如何编写一个函数我需要的东西。

我需要由国家进行排序,然后EVENT_TYPE,然后日期。

我的数组是这样的:

 阵列

    [0] =>排列
        (
            [ID] => 1
            [标题] =>无聊的会议
            [DATE_START] => 2010-07-30
            [TIME_START] => 06:45:下午
            [TIME_END] =>
            [状态] =>纽约
            [EVENT_TYPE] =>会议
        )    [1] =>排列
        (
            [ID] => 2
            [标题] =>寻找我的订书机
            [DATE_START] => 2010-07-22
            [TIME_START] => 10:45:调幅
            [TIME_END] =>
            [状态] =>纽约
            [EVENT_TYPE] =>会议
        )    [2] =>排列
        (
            [ID] => 3
            [标题] =>马里奥派对
            [DATE_START] => 2010-07-22
            [TIME_START] => 02:30:PM
            [TIME_END] => 07:15:下午
            [状态] =>纽约
            [EVENT_TYPE] =>派对
        )    [3] =>排列
        (
            [ID] => 4
            [标题] =>管道胶带党
            [DATE_START] => 2010-07-28
            [TIME_START] => 01:00:PM
            [TIME_END] =>
            [状态] =>加利福尼亚州
            [EVENT_TYPE] =>派对
        )
......等等


解决方案

您需要 在array_multisort

  $ MYLIST =阵列(
    阵列('ID'=大于1,'标题'=>'无聊的会议','EVENT_TYPE'=>会议),
    阵列('ID'=大于2,'标题'=>查找我的订书机','EVENT_TYPE'=>会议),
    阵列('ID'= GT; 3,'标题'=>马里奥派对,EVENT_TYPE'=>'党'),
    阵列('ID'=> 4,'标题'=>'管道胶带党','EVENT_TYPE'=>'党')
);#获得排序列的列表以及它们的数据传递给在array_multisort
$排序=阵列();
的foreach($ MYLIST为$ K => $ V){
    $排序['标题'] [$ K] = $ V ['标题'];
    $排序['EVENT_TYPE'] [$ K] = $ V ['EVENT_TYPE'];
}
#排序EVENT_TYPE递减,然后标题递增
在array_multisort($排序['EVENT_TYPE'],SORT_DESC,$排序['标题'],SORT_ASC,$ MYLIST);

I'm trying to sort a multidimensional array by multiple keys, and I have no idea where to start. I looked at uasort, but wasn't quite sure how to write a function for what I need.

I need to sort by the state, then event_type, then date.

My array looks like this:

    Array
(
    [0] => Array
        (
            [ID] => 1
            [title] => Boring Meeting
            [date_start] => 2010-07-30
            [time_start] => 06:45:PM
            [time_end] => 
            [state] => new-york
            [event_type] => meeting
        )

    [1] => Array
        (
            [ID] => 2
            [title] => Find My Stapler
            [date_start] => 2010-07-22
            [time_start] => 10:45:AM
            [time_end] => 
            [state] => new-york
            [event_type] => meeting
        )

    [2] => Array
        (
            [ID] => 3
            [title] => Mario Party
            [date_start] => 2010-07-22
            [time_start] => 02:30:PM
            [time_end] => 07:15:PM
            [state] => new-york
            [event_type] => party
        )

    [3] => Array
        (
            [ID] => 4
            [title] => Duct Tape Party
            [date_start] => 2010-07-28
            [time_start] => 01:00:PM
            [time_end] => 
            [state] => california
            [event_type] => party
        )
...... etc

解决方案

You need array_multisort

$mylist = array(
    array('ID' => 1, 'title' => 'Boring Meeting', 'event_type' => 'meeting'),
    array('ID' => 2, 'title' => 'Find My Stapler', 'event_type' => 'meeting'),
    array('ID' => 3, 'title' => 'Mario Party', 'event_type' => 'party'),
    array('ID' => 4, 'title' => 'Duct Tape Party', 'event_type' => 'party')
);

# get a list of sort columns and their data to pass to array_multisort
$sort = array();
foreach($mylist as $k=>$v) {
    $sort['title'][$k] = $v['title'];
    $sort['event_type'][$k] = $v['event_type'];
}
# sort by event_type desc and then title asc
array_multisort($sort['event_type'], SORT_DESC, $sort['title'], SORT_ASC,$mylist);

这篇关于排序多维数组由多个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆