I'm trying to encode 2 name/value pairs in a JSON object within a JSON array. At the moment I am able to encode the name/value pairs but each one goes in as a separate object within the array.
Here is my code:
if ($tag == "getLatLng")
{
$lineNumber = $_GET['lineNumber'];
$coordinates = $db->getCoOrds($lineNumber);
if ($coordinates != false)
{
//Get data and set success = 1
$response["success"] = 1;
$response['coordinates'] = array();
$arrayLength = count($coordinates);
foreach ($coordinates as $value)
//for ($i=0; $i<$arrayLength+1; $i++)
{
//echo $i;
$response["coordinates"][]['latitude'] = $value[0];
$response["coordinates"][]['longitude'] = $value[1];
//$response["coordinates"][]['longitude'] = $coordinates[$i];
}
echo json_encode($response);
}
This is a sample output:
{"success":1,"error":0,"coordinates":[{"latitude":"00.000000"},{"longitude":"-00.000000"},{"latitude":"00.000000"},{"longitude":"-00.000000"},{"latitude":"00.000000"},{"longitude":"-00.000000"}]}
But I need this:
{"success":1,"error":0,"coordinates":[{"latitude":"00.000000","longitude":"-00.000000"},{"latitude":"00.000000","longitude":"-00.000000"},{"latitude":"00.000000","longitude":"-00.000000"}]}
Thanks for your help!