I want to creat json array from mysql db,I try to use mysql query results to create json array object as below ,
$sql = "select DateTime ,Val1 from my table order by DateTime ASC ;";
$result = $db->query($sql);
$data = array();
$rowary = array();
while($row = mysqli_fetch_array($result))
{
$rowary['DateTime'] = $row['DateTime '] ;
$rowary['Val1'] = $row['Val1 '] ;
array_push($data,$rowary);
}
echo '<pre>' . var_export($data, true) . '</pre>';
the echo results is:
Array
(
[0] => Array
(
[DateTime] => 2017-02-09 12:27:23
[Val1] => 21.0333
)
[1] => Array
(
[DateTime] => 2017-02-09 16:18:13
[Val1] => 23.116699
)
)
but I want the results like this:
Array
(
Array
(
[DateTime] => 2017-02-09 12:27:23
[Val1] => 21.0333
)
Array
(
[DateTime] => 2017-02-09 16:18:13
[Val1] => 23.116699
)
)
please tell me how do I do that?
print_r($data[0]);. That would print the first array which holds2017-02-09 12:27:23as DateTime. The0,1,2....are the keys/indexes . You can't have arrays without keys.