2013-11-17 3 views
0

У меня есть массив [all_comments], который содержит 3 массива - [title], [content] и [date].Сортировка многомерного массива по дате php

Я хочу отсортировать [все_комментарии] по [дате]. Я пробовал многие функции сравнения даты на этом форуме, но ничего не работает для меня. Любая помощь или совет с благодарностью получили - спасибо!

Эти виды функции я пытался:

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

И это то, что мой массив выглядит следующим образом:

[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 

    ) 
) 

И это, как я вызываю функцию:

usort($all_comments,"date_compare"); 

И вот как я печатаю массив:

 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 />"; 
     } 

Ничего не распечатывается после сортировки, но без сортировки отпечатков несортированных массивов.

То, что я хочу, в конечном счете, является упорядоченным массивом, который выглядит следующим образом:

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

) 

ответ

0

Ответ обновление: Вашего массив имеет неправильную структуру. Сначала сделайте следующую модификацию:

<?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); 
+0

Большое спасибо за элегантное решение и извинения за мой запоздалый ответ. Мне удалось найти решение, сначала слияние заголовков, содержимого и массивов дат. Затем работала функция сортировки дат, и я смог распечатать элементы заголовка, даты и содержимого каждого из элементов i объединенного массива. –

+0

@BiswadipDasgupta Добро пожаловать :) Не забудьте принять ответ –

Смежные вопросы