简体   繁体   English

按数组值对多维数组排序

[英]Sort Multidimensional Array by Array Value

So I'm developing a message system for a project I'm working on and I want to organize an array of conversations by the latest message datetime. 因此,我正在为我正在从事的项目开发一个消息系统,我想在最新消息日期时间之前组织一系列对话。

Here's my array: 这是我的数组:

Array(
    [0] = Array (
        [ID] => 1,
        [USER] => 1,
        [AUTHOR] => 2,
        [TITLE] => Welcome to the Site!,
        [DATETIME] => 2016-09-12 20:41:16,
        [MESSAGES] => Array (
            [0] => Array (
                [ID] => 1,
                [CONVERSATION] => 1,
                [USER] => 1,
                [CONTENT] => Welcome to the site User!,
                [DATETIME] => 2016-09-13 00:19:20
                )
            [1] => Array (
                [ID] => 2,
                [CONVERSATION] => 1,
                [USER] => 2,
                [CONTENT] => Thanks for welcoming me!,
                [DATETIME] => 2016-09-13 00:27:54
                )
        )
    [1] = Array (
        [ID] => 2,
        [USER] => 2,
        [AUTHOR] => 1,
        [TITLE] => Hello World!,
        [DATETIME] => 2016-09-13 00:29:59,
        [MESSAGES] => Array (
            [0] => Array (
                [ID] => 1,
                [CONVERSATION] => 1,
                [USER] => 1,
                [CONTENT] => This is a test post.,
                [DATETIME] => 2016-09-13 00:19:45
                )
            [1] => Array (
                [ID] => 2,
                [CONVERSATION] => 1,
                [USER] => 2,
                [CONTENT] => This is an example response.,
                [DATETIME] => 2016-09-13 00:45:04
                )
        )
)

What I want to do is organise the array of conversations using the most recent message's datetime so the conversation with the most recent message will be first, the conversation with the second most recent message will be second and so on. 我要做的是使用最新消息的日期时间来组织一系列对话,因此与最新消息的对话将排在第一位,与第二最新消息的对话将排在第二位,依此类推。 I've been looking at different functions like 我一直在寻找不同的功能,例如

array_multisort
array_sort
usort

I've found other solutions online but they don't quite match what I'm looking for so does anyone know the best way to organize these chat messages? 我在网上找到了其他解决方案,但是它们与我在寻找的解决方案并不完全匹配,因此有人知道组织这些聊天消息的最佳方法吗?

Okay so after ages of searching other posts similar to mine and testing and modifying code I found the answer to my solution: 好吧,经过多年搜索其他类似我的帖子并测试和修改代码之后,我找到了解决方案的答案:

$datetimes = array();
$recentDate = 0;
foreach($conversations as $conversation) {
    foreach($conversation["MESSAGES"] as $message) {
        $curDate = strtotime($message["DATETIME"]);
        if($curDate > $recentDate) {
            $recentDate = $curDate;
        }
    }
    $datetimes[] = $recentDate;
}
array_multisort($datetimes, SORT_DESC, $conversations);

This sorts each the conversations based off the conversations with the most recent message. 这根据与最新消息进行的对话对每个对话进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM