简体   繁体   English

按日期php排序多维数组

[英]Sort a multidimensional array by date php

I have an array [all_comments] which contains 3 arrays - [title], [content] and [date]. 我有一个数组[all_comments],其中包含3个数组-[title],[content]和[date]。

I want to sort [all_comments] by [date]. 我想按[日期]对[all_comments]进行排序。 I have tried many of the date comparison functions on this forum but nothing seems to work for me. 我已经在该论坛上尝试了许多日期比较功能,但似乎没有任何效果。 Any help or advice gratefully received - thanks! 非常感谢您提供的任何帮助或建议-谢谢!

These are the kinds of function I have been trying: 这些是我一直在尝试的功能:

function date_compare($a, $b)
{
    $t1 = strtotime($a['date']);
    $t2 = strtotime($b['date']);
    return $t1 - $t2;
} 

And this is what my array looks like: 这就是我的数组的样子:

[all_comments] = Array 
( 
    [title] => Array 
    ( 
    [0] => bis posted an update in the group Strategic Resourcing
    [1] => bis posted a new activity comment 
    [2] => bis posted a new activity comment 
    [3] => bis posted an update 
    [4] => bis posted an update in the group Managing for Performance 
    ) 

    [content] => Array 
    ( 
    [0] => @david hey david the meeting earlier was very interesting - thanks! 
    [1] => Strategic Resourcing & Onboarding description.
    [2] => And another one to the original reply 
    [3] => This is a sample reply. 
    [4] => This is a new entry - does it go to the forum? 
    ) 

    [date] => Array 
    ( 
    [0] => 13-11-2013 5:36:48
    [1] => 24-10-2013 9:52:48 
    [2] => 12-11-2013 12:40:46
    [3] => 14-11-2013 2:26:04 
    [4] => 13-11-2013 5:39:49

    ) 
)

And this is how I am calling the function: 这就是我调用该函数的方式:

usort($all_comments,"date_compare");

And this is how I am printing the array: 这就是我打印数组的方式:

        for($k=0;$k<count($all_comments ['title']);$k++){
        echo ($all_comments ['title'][$k]) . "<br />"; 
        echo ($all_comments ['content'][$k]) . "<br />"; 
        echo ($all_comments ['date'][$k]) . "<br /><br />"; 
        echo "<br />"; 
        }

Nothing prints after sorting but without sorting the unsorted array prints fine. 排序后不打印任何内容,但不对未排序的数组进行排序就可以正常打印。

What I want ultimately is a sorted array which looks like this: 我最终想要的是一个排序数组,看起来像这样:

[sorted_array] = Array 
(
[0] =>  Array 
        (
        bis posted an update 
        This is a sample reply
        14-11-2013 2:26:04 
        )

[1] =>  Array 
        (
        bis posted an update in the group Managing for Performance  
        This is a new entry - does it go to the forum? 
        13-11-2013 5:39:49
        )

[2] =  Array 
        (
        bis posted an update in the group Strategic Resourcing
        @david hey david the meeting earlier was very interesting - thanks!
        13-11-2013 5:36:48
        )

[3] =>  Array 
        (
        bis posted a new activity comment 
        And another one to the original reply 
        12-11-2013 12:40:46
        )

[4] =>  Array 
        (
        bis posted a new activity comment 
        Strategic Resourcing & Onboarding description.
        24-10-2013 9:52:48 
        )

)

Answer updated: Your array has an incorrect structure. 已更新答案:您的阵列结构不正确。 Make the following modification to it first: 首先对其进行以下修改:

<?php
$all_comments = array
( 
    "title" => array 
    ( 
    0 => "bis posted an update in the group Strategic Resourcing",
    1 => "bis posted a new activity comment",
    2 => "bis posted a new activity comment",
    3 => "bis posted an update",
    4 => "bis posted an update in the group Managing for Performance",
    ),

    "content" => Array 
    ( 
    0 => "@david hey david the meeting earlier was very interesting - thanks!",
    1 => "Strategic Resourcing & Onboarding description.",
    2 => "And another one to the original reply",
    3 => "This is a sample reply.",
    4 => "This is a new entry - does it go to the forum?",
    ),

    "date" => Array 
    ( 
    0 => "13-11-2013 5:36:48",
    1 => "24-10-2013 9:52:48",
    2 => "12-11-2013 12:40:46",
    3 => "14-11-2013 2:26:04",
    4 => "13-11-2013 5:39:49",

    ),
);

$newArr = array();
foreach($all_comments as $masterKey => $masterValue) {
    foreach ($masterValue as $key => $value) {
      $newArr[$key][$masterKey] = $value;
    }
}

var_dump($newArr);

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

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