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:
$array1
and $array2
(e.g. as one array - say $array3
)[timestamp]
$array1 =
Array
(
[0] => stdClass Object
(
[id] => 100989
[fullname] => John Dobbs
[email] => [email protected]
[timestamp] => 1533116498
[uid] => 1349
)
[1] => stdClass Object
(
[id] => 32989
[fullname] => Terry White
[email] => [email protected]
[timestamp] => 1533116498
[uid] => 223
)
[2] => stdClass Object
(
[id] => 94883
[fullname] => Dan Rogers
[email] => [email protected]
[timestamp] => 1533116412
[uid] => 923
)
[3] => stdClass Object
(
[id] => 78382
[fullname] => Dan Rogers
[email] => [email protected]
[timestamp] => 1533111083
[uid] => 23
)
$array2 =
Array
(
[0] => stdClass Object
(
[id] => 100989
[fullname] => John Dobbs
[email] => [email protected]
[timestamp] => 1533111068
[aid] => 802531
[uid] => 1259
)
[1] => stdClass Object
(
[id] => 100989
[fullname] => John Dobbs
[email] => [email protected]
[timestamp] => 1533111063
[aid] => 802531
[uid] => 1259
)
[2] => stdClass Object
(
[id] => 155854
[fullname] => Terry White
[email] => [email protected]
[timestamp] => 1533088445
[aid] => 802069
[uid] => 1833
)
[3] => stdClass Object
(
[id] => 165212
[fullname] => Dan Rogers
[email] => [email protected]
[timestamp] => 1533080179
[aid] => 801761
[uid] => 1831
)
[4] => stdClass Object
(
[id] => 116940
[fullname] => Dan Rogers
[email] => [email protected]
[timestamp] => 1533059095
[aid] => 801343
[uid] => 1239
)
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");
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments