合并多维数组,然后按二级项排序

时间:2014-08-15 20:25:31

标签: php arrays sorting date-sorting

所以我有了这些有趣的多维数组,我需要合并和排序。

这是一个例子......

$arr1 = array(
    array(
        'date' => '2014-08-14 18:07:28',
        'message' => "Hi there!"
    ),
    array(
        'date' => '2014-08-14 17:07:28',
        'message' => "Hi there!"
    )
);

$arr2 = array(
    array(
        'date' => '2014-08-14 18:01:28',
        'message' => "Hi there!"
    ),
    array(
        'date' => '2014-08-14 11:07:28',
        'message' => "Hi there!"
    )
);

我需要按照新数组中第0项的最新日期对它们进行排序。

尝试使用usort和自定义函数,但没有太多运气。

这就是我现在所拥有的自定义功能。

function compareDate($ad, $bd) 
{
    $ad_time = strtotime($ad['date']);
    $bd_time = strtotime($bd['date']);
    return ($ad_time - $bd_time);
}

如何改善此功能?

这是我合并和排序的地方。

$mergedarray = array_merge($array1, $array2);
usort($mergedarray, 'compareDate');

0 个答案:

没有答案
相关问题