I'm trying to print names of movie
and music
in DESC
order according to the datetime
of the rows. So I need some algorithms help in inserting into an array according and have it ordered.
The database:
Movie Music
---------------------------------- ---------------------------------------
| title | row_insert_dt | | title | row_insert_dt |
---------------------------------- ---------------------------------------
| Iron Man | 2019-08-21 08:56:00 | | Lose Yourself | 2019-08-21 08:56:01 |
| Thor | 2019-08-21 08:56:02 | | Rap God | 2019-08-21 08:56:03 |
| ... | ... | | ... | ... |
| ... | ... | | ... | ... |
The code:
<?php
$output = array();
$connection = new PDO("mysql:host=localhost;charset=UTF8;dbname=test;", "root", "");
// Movie
$statement = $connection->prepare("SELECT title, row_insert_dt FROM movie ORDER BY row_insert_dt DESC;");
$statement->execute();
foreach($statement->fetchAll() as $y) {
array_push($output, array("title" => $y["title"], "date" => $y["row_insert_dt"]));
}
// Music
$statement = $connection->prepare("SELECT title, row_insert_dt FROM music ORDER BY row_insert_dt DESC;");
$statement->execute();
foreach($statement->fetchAll() as $y) {
// How to Insert into '$output' in sorted order.
}
echo json_encode($output);
?>
My desirable output:
[
["Rap God", "2019-08-21 08:56:03"],
["Thor", "2019-08-21 08:56:02"],
["Lose Yourself", "2019-08-21 08:56:01"],
["Iron Man", "2019-08-21 08:56:00"]
]