0

嗨,我有一个数组,它是 2 个独立数组的组合,产生相同的结果,以下是合并后的数组:

Array
(
    [0] => Array
        (
            [event_id] => 16
            [user_id] => 3
            [date_created] => 20130227095010

        )

    [1] => Array
        (
            [event_id] => 15
            [user_id] => 2
            [date_created] => 20130227101734

        )

    [2] => Array
        (
            [event_id] => 16
            [user_id] => 2
            [date_created] => 20130227094856

        )

)

从这里开始,如何在不使用之前合并的 2 个数组的情况下根据 date_created desc 或 asc 再次排序?

更新:先前的数组结果

数组1:

Array
(
    [0] => Array
        (
            [event_id] => 15
            [user_id] => 2
            [date_created] => 20130227101734


        )

    [1] => Array
        (
            [event_id] => 16
            [user_id] => 2
            [date_created] => 20130227094856

        )

)

数组 2:

Array
(
    [0] => Array
        (
            [event_id] => 16
            [user_id] => 3
            [date_created] => 20130227095010

        )

)

我如何在这些函数中使用循环来为上述每个数组提取 date_created

usort($array,function($a, $b) { return $a['date_created'] - $b['date_created']; }); 
4

3 回答 3

2

很容易用类似的东西usort()

usort($array,
      function($a, $b) { return $a['date_created'] - $b['date_created']; });
于 2013-02-27T02:31:53.363 回答
1

您可以创建自己的排序函数回调并将其与usort方法一起使用。需要重新使用前 2 个数组。

usort($yourMergedArray, yourCallBackSort);

function yourCallBackSort($firstEntry, $secondEntry)
{
    // Do some stuff to compare your values and return -1, 0 or 1
    // depend if $firstEntry id <, = or > to $secondEntry
}
于 2013-02-27T02:31:33.583 回答
0

尝试这个,

$sort = array();
foreach($your_combined_array as $k=>$v) {
    $sort['date_created'][$k] = $v['date_created'];
}

array_multisort($sort['date_created'], SORT_DESC, $your_combined_array);


echo "<pre>";
print_r($your_combined_array);
于 2013-02-27T04:38:55.120 回答