简体   繁体   English

PHP和JSON:返回合适的格式

[英]PHP and JSON: Returning a suitable format

I am trying to use google map direction API to map route connecting cities from the database. 我正在尝试使用Google Map Direction API来从数据库映射连接城市的路线。 My problem is that I am stuck at a point I am supposed to return values from php script through json. 我的问题是我被困在应该通过json从php脚本返回值的地方。 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() PHP使用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. 中点是选择了$ start和$ end之后剩余的所有值。 Any ideas is highly appreciated.Thanks 任何想法都将受到高度赞赏。

$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! 注意, array_shiftarray_pop修改$data数组!

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"
}

When you get the response back from PHP, it will be a string, containing JSON formatted data. 当您从PHP获得响应时,它将是一个包含JSON格式数据的字符串。

You will need to use: 您将需要使用:

var myObject = JSON.parse(stringOfJson);

To convert it to a JSON object. 将其转换为JSON对象。

If you want a PHP representation of the data, why not create an object in PHP to represent it: 如果要用PHP表示数据,为什么不用PHP创建一个对象来表示它:

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. 然后,您可以将此对象序列化为JSON,并将采用您需要的格式。

$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. 您可以更进一步,并创建一个Waypoint类来表示数组中的每个Waypoint。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM