I am trying to store all values in a table called locations from MySQL database which looks like

into a PHP Array called $locations = array(); .I need to store the data in a format(Associated array or Regular) which I can convert them to a JSON file by using the php's json_encode() function. The final output must looks like
{
"markers": [{
"id": 1,
"type": "shelter",
"lat": 55.6295639,
"long": 12.6392556,
"latlong": "55.6295639,12.6392556"
}, {
"id": 2,
"type": "shelter",
"lat": 49.6125639,
"long": 12.6392556,
"latlong": "55.6295639,12.6392556"
}, {
"id": 3,
"type": "shelter",
"lat": 56.6786339,
"long": 11.6392556,
"latlong": "55.6295639,12.6392556"
}, {
"id": 4,
"type": "shelter",
"lat": 51.6295639,
"long": 13.6392556,
"latlong": "55.6295639,12.6392556"
}, ]
}
I already tried this code but not sure I am doing right? or how to export it to json?
$result = mysql_query('select * from locations');
$locations = array();
while($r = mysql_fetch_array($result) {
$row = array();
foreach($r as $k=>$v) {
$row[$k] = $v;
}
array_push($locations,$row);
unset($row);
}
var_dump($locations)2.json_encode3. you don't needunsetthere 4. You don't need a loop - just$locations[] = $r;