I have the next PHP code that makes me a JSON file with data from my SQL table:
<?php
$mysqli = new mysqli('localhost','root','xxxx','xxxx');
$myArray = array();
$result = $mysqli->query("SELECT CONCAT(Datum , ' ', Tijd) as Datum, KleurL FROM metingen order by Datum ASC limit 20");
if ($result) {
$tempArray = array();
while ($row = $result->fetch_object()) {
$tempArray = $row;
array_push($myArray, $tempArray);
}
echo json_encode($myArray,JSON_NUMERIC_CHECK);
$fp = fopen('resultsv2.json', 'w');
fwrite($fp, json_encode($myArray,JSON_NUMERIC_CHECK));
fclose($fp);
}
$result->close();
$mysqli->close();
?>
The ouput of the JSON file looks like this.:
[{"Datum":"17-01-2019 10:31:39","KleurL":17.68},{"Datum":"17-01-2019 11:10:59","KleurL":71.76},{"Datum":"18-01-2019 08:40:41","KleurL":70.7},{"Datum":"18-01-2019 10:30:01","KleurL":71.03},{"Datum":"18-01-2019 12:05:46","KleurL":70.56},{"Datum":"18-01-2019 14:31:58","KleurL":16.2}]
But I would like to see that the output of that file looks like this.:
[
[
17.68,
17-01-2019 10:31:39
],
[
18.11,
17-01-2019 11:15:20
]
]
How can I get this output in the JSON file?
You can use the json_encode() JSON_PRETTY_PRINT
options:
json_encode($myArray,JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
edit:
if you are using windows, you need to change the carriage return using something like this:
$myArray = array("test" => "data");
$buffer = json_encode($myArray,JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
echo str_replace("\n", "\r\n", $buffer);
// or
$fp = fopen('resultsv2.json', 'w');
fwrite($fp, str_replace("\n", "\r\n", $buffer));
fclose($fp);
If you are printing this into the browser you need to use <pre>
tags
<pre>
echo json_encode($myArray,JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
</pre>
The following code should achieve what you are trying to do. This code was tested on my pc and seems to be working perfectly.
<?php $mysqli = new mysqli('localhost', 'root', 'xxxx', 'xxxx');
if ($result = $mysqli->query("SELECT CONCAT(Datum , ' ', Tijd) as Datum, KleurL FROM metingen order by Datum ASC limit 20")) {
$current_row_number = 0;
$output = "[";
while ($row = $result->fetch_object()) {
if ($current_row_number > 0) {
$output = $output . ","; //add comma between each row
}
$output = $output . "[" . $row->KleurL . "," . $row->Datum . "]"; //add each row
$current_row_number++;
}
$output = $output . "]";
echo $output;
$fp = fopen('resultsv2.json', 'w');
fwrite($fp, $output);
fclose($fp);
}
$result->close();
$mysqli->close();
If you want to format the output nicely, use the following code:
<?php $mysqli = new mysqli('localhost', 'root', '244466666', 'tkd');
if ($result = $mysqli->query("SELECT CONCAT(Datum , ' ', Tijd) as Datum, KleurL FROM metingen order by Datum ASC limit 20")) {
$current_row_number = 0;
$output = "[";
while ($row = $result->fetch_object()) {
if ($current_row_number > 0) {
$output = $output . ","; //add comma between each row
}
$output = $output . "\n\t" . "[" . "\n\t\t" . $row->KleurL . "," . "\n\t\t" . $row->Datum . "\n\t" . "]"; //add each row
$current_row_number++;
}
$output = $output . "\n]";
// echo $output; //if you want to see the output in the terminal/command prompt
echo "<pre>" . $output . "</pre>"; //if you want to see the output in a browser
$fp = fopen('resultsv2.json', 'w');
fwrite($fp, $output);
fclose($fp);
}
$result->close();
$mysqli->close();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.