附加两个多维数组,然后在PHP中按时间戳记值排序

时间:2018-08-01 11:27:44

标签: php arrays multidimensional-array

我有两个多维数组,我想先合并(追加),然后再按时间戳值对它们进行排序。

下面是两个已经按其单元格值[timestamp]排序的数组。

我想做的是:

  • 合并$array1$array2(例如,作为一个数组-说$array3
  • [timestamp]对一个大数组进行排序

这是两个数组:

$array1 = 

Array
(
    [0] => stdClass Object
        (
            [id] => 100989
            [fullname] => John Dobbs
            [email] => john_dobbs@email.com
            [timestamp] => 1533116498
            [uid] => 1349
        )
    [1] => stdClass Object
            (
                [id] => 32989
                [fullname] => Terry White
                [email] => some@email.com
                [timestamp] => 1533116498
                [uid] => 223
            )
    [2] => stdClass Object
            (
                [id] => 94883
                [fullname] => Dan Rogers
                [email] => some@email.com
                [timestamp] => 1533116412
                [uid] => 923
            )
    [3] => stdClass Object
            (
                [id] => 78382
                [fullname] => Dan Rogers
                [email] => some@email.com
                [timestamp] => 1533111083
                [uid] => 23
            )


$array2 = 
Array
(
    [0] => stdClass Object
        (
            [id] => 100989
            [fullname] => John Dobbs
            [email] => john_dobbs@email.com
            [timestamp] => 1533111068
            [aid] => 802531
            [uid] => 1259
        )
    [1] => stdClass Object
        (
            [id] => 100989
            [fullname] => John Dobbs
            [email] => john_dobbs@email.com
            [timestamp] => 1533111063
            [aid] => 802531
            [uid] => 1259
        )
    [2] => stdClass Object
            (
                [id] => 155854
                [fullname] => Terry White
                [email] => some@email.com
                [timestamp] => 1533088445
                [aid] => 802069
                [uid] => 1833
            )
    [3] => stdClass Object
            (
                [id] => 165212
                [fullname] => Dan Rogers
                [email] => some@email.com
                [timestamp] => 1533080179
                [aid] => 801761
                [uid] => 1831
            )
    [4] => stdClass Object
            (
                [id] => 116940
                [fullname] => Dan Rogers
                [email] => some@email.com
                [timestamp] => 1533059095
                [aid] => 801343
                [uid] => 1239
            ) 

1 个答案:

答案 0 :(得分:2)

我认为下面的代码段应该对您有用,

$array = array_merge($array1, $array2);

function cmp($a, $b)
{
    return strcmp($a->timestamp, $b->timestamp);
}

usort($array, "cmp");