douhuike3199 2016-09-18 07:39
浏览 41
已采纳

按数组值排序多维数组

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?

  • 写回答

1条回答 默认 最新

  • doq1969 2016-09-24 05:46
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?