按多列对多维数组进行排序 [英] Sort multidimensional array by multiple columns

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

问题描述

我正在尝试按多个键对多维数组进行排序,但我不知道从哪里开始.我查看了 uasort(),但不太确定如何编写满足我需要的函数.

我需要按 state 排序,然后是 event_type,然后是 date_start.

我的数组如下所示:

<预><代码>[['ID' =>1, '标题' =>'无聊的会议', 'date_start' =>'2010-07-30', 'event_type' =>'会议', '状态' =>'纽约'],['ID' =>2、'标题' =>'查找我的订书机', 'date_start' =>'2010-07-22', 'event_type' =>'会议', '状态' =>'纽约'],['ID' =>3、'标题' =>'马里奥派对', 'date_start' =>'2010-07-22', 'event_type' =>'党', '州' =>'纽约'],['ID' =>4、'标题' =>'Duct Tape Party', 'date_start' =>'2010-07-28', 'event_type' =>'党', '州' =>'加利福尼亚']]

我想要的结果是:

<预><代码>[['ID' =>4、'标题' =>'Duct Tape Party', 'date_start' =>'2010-07-28', 'event_type' =>'党', '州' =>'加利福尼亚']['ID' =>2、'标题' =>'查找我的订书机', 'date_start' =>'2010-07-22', 'event_type' =>'会议', '状态' =>'纽约'],['ID' =>1, '标题' =>'无聊的会议', 'date_start' =>'2010-07-30', 'event_type' =>'会议', '状态' =>'纽约'],['ID' =>3、'标题' =>'马里奥派对', 'date_start' =>'2010-07-22', 'event_type' =>'党', '州' =>'纽约'],]

解决方案

您需要 array_multisort

$mylist = array(array('ID' => 1, 'title' => '无聊的会议', 'event_type' => '会议'),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'));# 获取要传递给 array_multisort 的排序列及其数据的列表$sort = array();foreach($mylist as $k=>$v) {$sort['title'][$k] = $v['title'];$sort['event_type'][$k] = $v['event_type'];}# 按 event_type desc 排序,然后按标题 ascarray_multisort($sort['event_type'], SORT_DESC, $sort['title'], SORT_ASC,$mylist);

从 PHP 5.5.0 开始:

array_multisort(array_column($mylist, 'event_type'), SORT_DESC,array_column($mylist, 'title'), SORT_ASC,$mylist);

$mylist 现在是:

数组 (0 =>大批 ('ID' =>4、'标题' =>'胶带派对','事件类型' =>'派对',),1 =>大批 ('ID' =>3、'标题' =>《马里奥派对》,'事件类型' =>'派对',),2 =>大批 ('ID' =>1、'标题' =>'无聊的会议','事件类型' =>'会议',),3 =>大批 ('ID' =>2、'标题' =>'找到我的订书机','事件类型' =>'会议',),)

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_start.

My array looks like this:

[
    ['ID' => 1, 'title' => 'Boring Meeting',  'date_start' => '2010-07-30', 'event_type' => 'meeting', 'state' => 'new-york'],
    ['ID' => 2, 'title' => 'Find My Stapler', 'date_start' => '2010-07-22', 'event_type' => 'meeting', 'state' => 'new-york'],
    ['ID' => 3, 'title' => 'Mario Party',     'date_start' => '2010-07-22', 'event_type' => 'party',   'state' => 'new-york'],
    ['ID' => 4, 'title' => 'Duct Tape Party', 'date_start' => '2010-07-28', 'event_type' => 'party',   'state' => 'california']
]

My desired result is:

[
    ['ID' => 4, 'title' => 'Duct Tape Party', 'date_start' => '2010-07-28', 'event_type' => 'party',   'state' => 'california']
    ['ID' => 2, 'title' => 'Find My Stapler', 'date_start' => '2010-07-22', 'event_type' => 'meeting', 'state' => 'new-york'],
    ['ID' => 1, 'title' => 'Boring Meeting',  'date_start' => '2010-07-30', 'event_type' => 'meeting', 'state' => 'new-york'],
    ['ID' => 3, 'title' => 'Mario Party',     'date_start' => '2010-07-22', 'event_type' => 'party',   'state' => 'new-york'],
]

解决方案

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);

As of PHP 5.5.0:

array_multisort(array_column($mylist, 'event_type'), SORT_DESC,
                array_column($mylist, 'title'),      SORT_ASC,
                $mylist);

$mylist is now:

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

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

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