0

I have two multi dimensional arrays that I wanna first combine (append) and then sort all by a timestamp value.

Below are the two arrays that are already sorted by their cell value [timestamp].

What I want to do is:

  • Combine $array1 and $array2 (e.g. as one array - say $array3)
  • Sort that one large array by [timestamp]

Here are both arrays:

$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
            ) 
Steve
  • 2,546
  • 8
  • 49
  • 94

1 Answers1

2

I think following snippet should work for you,

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

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

usort($array, "cmp");
Harsh Virani
  • 367
  • 3
  • 8