PHP / SQL - Select data from two tables, from two databases and sort them by time


I am trying to merge data from two tables, from two different databases together and sort the result by time.

I am using PDO in my code, and I can connect to the two databases like this: $dbh (database1) and $Ndbh (database2)

Here is where I get confused. I know I can use the two database selectors mentioned above to get data from each database - but how do I merge the data?

This should work :

$query1 = $dbh->query('SELECT * FROM table1')->fetchAll(PDO::FETCH_ASSOC);
$query2 = $Ndbh ->query('SELECT * FROM table2')->fetchAll(PDO::FETCH_ASSOC);

$data = array_merge($query1, $query2);

usort($data , function($a, $b) {
    return $a['time'] - $b['time'];