我是 php 新手,我正在尝试使用 json 数据和二维数组。
我已经将一个 json 对象从谷歌距离矩阵 API 转换为一个数组。我想将起点和终点地址作为二维矩阵中的键,然后是数组中的距离。
实际上,它看起来像:
origin1 origin2
dest1 2 3
dest2 10 1
我也有单独变量中的位置。我以为我可以将这些变量设置为二维数组中的键,但这似乎不起作用。
我想我应该只能从我转换为数组的 json 中完成所有这些工作,但我不确定如何。结果我可能把事情复杂化了。
任何帮助都会很棒。谢谢!
<?php
include_once('dbHandle.php');
$pcSet1 = getString();
$url="http://maps.googleapis.com/maps/api/distancematrix/json?origins=London,UK|Leeds,UK&destinations=London,UK|Leeds,UK&mode=driving&language=en-UK&UNITS=imperial&sensor=false";
$json = file_get_contents($url); // get the data from Google Maps API
$result = json_decode($json, true); // convert it from JSON to php array
$origin_addresses = array();
for($i=0; $i<sizeof($result); $i++){
$origin_addresses = $result['origin_addresses'];
}
$dist = array_flip($origin_addresses);
//2d array to look like this but with Origin replaced with location and distances from the json
$arr = array("origin1" => array("dest1" => "1 mile", "dest2"=>"5 miles", "dest3"=>"10 miles" ),
"origin2" => array("dest1" => "8 mile", "dest2"=>"6 miles", "dest3"=>"11 miles" )
);
?>