mysql - PHP array comma separated string

I have a question. I try to separate the output from my MySQL database with a comma. Somehow that doesn't work.

PHP code:

echo "[";

if ($result = $con->query("SELECT * FROM table")) {

    while($row = $result->fetch_array(MYSQLI_ASSOC)) {
        $timestamp = strtotime($row['datetime']);


            echo "[" . $timestamp . "000" . "," . $row['temp'] . "]" . ",";


}
}
$result->close();
$con->close();
echo "]";

Output:

[[1591072800000,12.5],[1591073100000,12.6], ........ [1591367100000,21.6],[1591367160000,21.5],]

The last comma is too much and has to go.

Does anyone have an approach to this?

Thank you very much

PS.:

I have already tried it with implode (), but without success.

PHP code:

$arr =  array("[" . $timestamp . "000" . "," . $row['temp'] . "]");

        echo implode(", ",$arr);

Output:

[[1591072800000,12.5][1591073100000,12.6]........[1591367580000,20.7][1591367640000,20.5]]

Answer

Solution:

Why not put what you want into a proper array and then echo the json?

$output = array();
if ($result = $con->query("SELECT * FROM table")) {
    while($row = $result->fetch_array(MYSQLI_ASSOC)) {
        $output[] = array(strtotime($row['datetime']) . "000", $row['temp']);
    }
}
$result->close(); $con->close();
echo json_encode($output);`

Source