felix cheruiyot
felix cheruiyot

Reputation: 311

PHP and JSON: Returning a suitable format

I am trying to use google map direction API to map route connecting cities from the database. My problem is that I am stuck at a point I am supposed to return values from php script through json. My data to map is inform of an array:

$data=array('chicago','new york','lebanon','maysvile','greenfield');

My intention is to return the following format from my data array.

var request = {
   origin:start, 
   destination:end,
   waypoints:[{
         location:"",
         stopover:true
   }],
   travelMode: google.maps.DirectionsTravelMode.DRIVING
}

This is how I got my start and destination: the first and last elements in the array:

$start=reset($data);     
$end=end($data);

Data to returned by php using json_encode()

    $response=array('origin'=>$start,'destination'=>$end,'travelMode'=>"google.maps.DirectionsTravelMode.DRIVING");
echo json_encode($response);

The format returned is not correct. Also I can't figure out how I should do the mid points. The mid points are all the values remaining after the $start and $end have been picked. Any ideas is highly appreciated.Thanks

Upvotes: 0

Views: 204

Answers (2)

Fenton
Fenton

Reputation: 251172

When you get the response back from PHP, it will be a string, containing JSON formatted data.

You will need to use:

var myObject = JSON.parse(stringOfJson);

To convert it to a JSON object.

If you want a PHP representation of the data, why not create an object in PHP to represent it:

class RouteInformation 
{
    public $Origin;
    public $Destination;
    public $Waypoints;
    public $TravelMode;

    public function __construct() 
    {
        $this->Waypoints = array();
    }
}

You can then serialize this object to JSON and it will be in the format you require.

$response = new RouteInformation();
$response->Origin = array_shift($data);
$response->Destination = array_pop($data);
$response->TravelMode = 'DRIVING';

foreach($data as $wp) {
    $response->Waypoints[] = array('stopover' => true, 'location' => $wp);
}

echo json_encode($response);

You could go a step further and create a Waypoint class to represent each Waypoint in the array.

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318638

$response = array(
    'origin' => array_shift($data),
    'destination' => array_pop($data),
    'waypoints' => array(),
    'travelMode' => 'DRIVING'
);
foreach($data as $wp) {
    $response['waypoints'][] = array('stopover' => true, 'location' => $wp);
}
echo json_encode($response);

Note that array_shift and array_pop modify the $data array!

The output of the script is:

{
    "origin": "chicago",
    "destination": "greenfield",
    "waypoints": [
        {
            "stopover": true,
            "location": "new york"
        },
        {
            "stopover": true,
            "location": "lebanon"
        },
        {
            "stopover": true,
            "location": "maysvile"
        }
    ],
    "travelMode": "DRIVING"
}

Upvotes: 1

Related Questions