我已经搜索了互联网并堆叠了几个小时,但我无法找到问题的答案。
我有一个带有邮政编码的数组,想要检查它们之间的距离。
function afstand($postcode, $postcodeBestemming) {
$url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=" . $postcode . "&destinations=" . $postcodeBestemming . "&mode=driving&language=en-EN&sensor=false";
$data = @file_get_contents($url);
$result = json_decode($data, true);
print_r($result); //outputs the array
}
我的数组看起来像这样:
id klant_id datum datum_afspraak postcode max_afstand
1 1 2016-07-13 2016-07-21 5263CL 90
2 2 2016-07-13 2016-07-29 2344HA 20
3 3 2016-07-21 2016-07-26 3546AR 27
当我用foreach循环遍历它们时,它只填充第一个数组元素。
foreach($klanten as $klant) {
afstand('3746AB', $klant['postcode']);
}
第一个元素很好,但是当涉及到第二个元素时,目的地是空的
Array ( [destination_addresses] => Array ( [0] => ) [origin_addresses] => Array ( [0] => 5521 KL Eersel, Netherlands ) [rows] => Array ( [0] => Array ( [elements] => Array ( [0] => Array ( [status] => NOT_FOUND ) ) ) ) [status] => OK )
无论我如何格式化数组,它都不会填充目标
编辑: 对于请求,这是完整的代码:
foreach($offertes as $offerte) {
print $garagePostcode;
print '<br/>';
print $offerte['max_afstand'];
print '<br/>';
print $offerte['id'];
print '<br/>';
print $offerte['postcode'];
print '<br/>';
$afstand = afstand($garagePostcode, $offerte['postcode']);
//print $afstand;
var_dump($offerte['postcode']);
print '<br/>';
print '<br/>';
unset($postcode);
}
功能
function afstand($postcode, $postcodeBestemming) {
$url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=" . $postcode . "&destinations=" . $postcodeBestemming . "&mode=driving&language=en-EN&sensor=false";
$data = @file_get_contents($url);
$result = json_decode($data, true);
print_r($result); //outputs the array
$distance = $result["rows"][0]["elements"][0]["distance"]["value"] / 1000;
return $distance;
}
输出:
id klant_id datum datum_afspraak postcode max_afstand
1 1 2016-07-13 2016-07-21 5527CL 90
2 2 2016-07-13 2016-07-29 2344HA 20
3 3 2016-07-21 2016-07-26 3546AR 27
5521KL
90
1
5527CL
Array ( [destination_addresses] => Array ( [0] => 5527 CL Hapert, Netherlands ) [origin_addresses] => Array ( [0] => 5521 KL Eersel, Netherlands ) [rows] => Array ( [0] => Array ( [elements] => Array ( [0] => Array ( [distance] => Array ( [text] => 6.1 km [value] => 6112 ) [duration] => Array ( [text] => 12 mins [value] => 706 ) [status] => OK ) ) ) ) [status] => OK ) string(6) "5527CL"
5521KL
20
2
2344HA
Array ( [destination_addresses] => Array ( [0] => ) [origin_addresses] => Array ( [0] => 5521 KL Eersel, Netherlands ) [rows] => Array ( [0] => Array ( [elements] => Array ( [0] => Array ( [status] => NOT_FOUND ) ) ) ) [status] => OK )
Notice: Undefined index: distance in /var/www/rama/functions.php on line 176
string(6) "2344HA"
5521KL
27
3
3546AR
Array ( [destination_addresses] => Array ( [0] => ) [origin_addresses] => Array ( [0] => 5521 KL Eersel, Netherlands ) [rows] => Array ( [0] => Array ( [elements] => Array ( [0] => Array ( [status] => NOT_FOUND ) ) ) ) [status] => OK )
Notice: Undefined index: distance in /var/www/rama/functions.php on line 176
string(6) "3546AR"