【问题标题】:Sort PHP bidimensional array by the values of the inner arrays of a specific key [duplicate]按特定键的内部数组的值对PHP二维数组进行排序[重复]
【发布时间】:2019-01-12 04:41:43
【问题描述】:

我有以下多个一维关联数组的二维索引数组:

Array (
    [0] => Array (
            [from] => Person 1
            [to] => Person 2
            [platform] => Instagram Direct Messaging
            [date] => 2016/06/27
            [time] => 12:00
            [ampm] => PM
            [specialcontent] => none
            [content] => Hello
     )
    [1] => Array (
            [from] => Person 1
            [to] => Person 2
            [platform] => Instagram Direct Messaging
            [date] => 2016/06/27
            [time] => 12:00
            [ampm] => PM
            [specialcontent] => none
            [content] => How are you?
     )
    [2] => Array (
            [from] => Person 2
            [to] => Person 1
            [platform] => Instagram Direct Messaging
            [date] => 2016/06/27
            [time] => 6:00
            [ampm] => PM
            [specialcontent] => none
            [content] => Oh, hey there. I'm fine
     )
    [3] => Array (
            [from] => Person 2
            [to] => Person 1
            [platform] => Instagram Direct Messaging
            [date] => 2016/06/27
            [time] => 6:01
            [ampm] => PM
            [specialcontent] => none
            [content] => What about you?
     )
)

我想按其中的date 字段(键)的值对内部数组进行排序,这意味着,我必须按年、月、日对它们进行升序排序命令。我可以分别使用以下命令从date(键)获取这些值:$year = substr($item['date'], 0, 4);$month = substr($item['date'], 5, 2);$day = substr($item['date'], -2);。我也知道我可能需要使用usortarray_multisort 之类的函数,但我想不出如何创建一个函数来按年、月、日返回数组的顺序@ 987654329@键。

【问题讨论】:

  • 顺便说一句,它们已经在我的代码中订购了。假设它们是无序的。
  • 不行吗? usort($arr, function ($a, $b) { return $a['date'] <=> $b['date']; });
  • @AntoineB 如果 Nikita U. 的评论有效,我认为这是重复的,我现在将对其进行测试。然后我会评论结果。
  • @NikitaU。我不知道 PHP 是如何做到的,但它确实有效。那么,我应该删除这个问题吗?我已经得到了答案(谢谢你),但没有人回答。也许你可以用那个评论来回答,我会选择你的答案。
  • @Machavity 这与你所说的重复的问题不太一样。

标签: php arrays


【解决方案1】:

@Nikita 在问题中的评论有效。在下面的代码中,我还考虑了按时间(包括 AM/PM 标签)对消息进行排序:

        // Sort messages by date
            usort($MessagesArray, function ($a, $b) {
                return $a['date'] <=> $b['date'];
            });
        // Sort messages by date and time
            // Obtain a list of columns
            foreach ($MessagesArray as $key => $row) {
                $date[$key]  = $row['date'];
                $time[$key] = $row['time'];
            }
            // Sort the messages
            array_multisort($date, SORT_ASC, $time, SORT_ASC, $MessagesArray);

请记住,@Scoots' 和 @Elementary 的 cmets 也已经按日期对它们进行了排序。

【讨论】:

    【解决方案2】:

    一个可能更简单的实现,也使用 usort,但利用函数 strtotime - 我们将所有日期/时间字段连接在一起,将它们转换为 unix 时间戳并升序排序。

    usort($my_array, function($a, $b) {
        $a = strtotime("{$a['date']} {$a['time']} {$a['ampm']}");
        $b = strtotime("{$b['date']} {$b['time']} {$b['ampm']}");
    
        if($a == $b) {
            return 0;
        }
    
        return $a < $b ? -1 : 1;
    });
    

    【讨论】:

    • 上帝保佑你,我的朋友。我的真正意图是还按日期对消息进行排序,然后按时间(包括 am/pm 标签)对消息进行排序,这意味着我不需要按三个标准/键对数组进行排序。但是您已经完成了,我已经对其进行了测试,并且工作正常!所以谢谢
    【解决方案3】:

    你可以这样使用 usort:

    function date_sort($a,$b){
        list($a_year,$a_month,$a_day)=explode('/',$a);
        list($b_year,$b_month,$b_day)=explode('/',$b);
        if($a_year>$b_year) return 1;
        if($a_year<$b_year) return -1;
        if($a_month>$b_month) return 1;
        if($a_month<$b_month) return -1;
        if($a_day>$b_day) return 1;
        if($a_day<$b_day) return -1;
        return 0;
    }
    
    function time_sort($a,$b){
        list($a_hour,$a_min)=explode(':',$a);
        list($b_hour,$b_min)=explode(':',$b);
        if($a_hour>$b_hour) return 1;
        if($a_hour<$b_hour) return -1;
        if($a_min>$b_min) return 1;
        if($a_min<$b_min) return -1;
        return 0;
    }
    
    function ampm_sort($a,$b){
        if($a[0]===$b[0]) return 0;
        if(strtolower($a[0])==='a'&&strtolower($b[0])==='p') return -1;
        else return 1;
    }
    
    function customSort($a,$b){
        if(date_sort($a['date'],$b['date'])>0){
            return 1;
    
        }elseif(date_sort($a['date'],$b['date'])<0){
            return -1;
    
        }else{
            if(ampm_sort($a['ampm'],$b['ampm'])>0)
                return 1;
            if(ampm_sort($a['ampm'],$b['ampm'])<0)
                return -1;
            if(ampm_sort($a['ampm'],$b['ampm'])===0){
                if(time_sort($a['time'],$b['time'])>0){
                    return 1;
                }elseif(time_sort($a['time'],$b['time'])<0){
                    return -1;
                }else{
                    return 0;
                }
            }
        }
    
    
    }
    

    如果我们假设您将数组命名为 $my_array ,则可以使用以下命令对其进行排序:

    usort($my_array,'customSort');
    

    这样,您只需先按年排序数组,然后按月排序,然后按天排序,然后按时间排序。

    【讨论】:

    • @alej27 我认为你可能也需要这个,因为如果你只按日期对数组进行排序,它不会真正被排序,因为如果你尝试做某事,你可能还需要按时间和时刻排序像聊天...
    • 也感谢您按时间排序(包括 am/pm 标签)!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 2011-05-29
    相关资源
    最近更新 更多