我正在构建一个函数,从谷歌地图链接中获取纬度和经度。我让它工作了它应该如何工作它应该如何:
public function getLocaties(){
$locations = $this->getObjects(68);
foreach ($locations as $location) {
$latAndLong = $this->getLatAndLong($locations ['data']['link']);
$location['data']['latiude'] = $latAndLong['lat'];
$location['data']['longtitude'] = $latAndLong['long'];
var_dump($locatie);
}
return $locaties;
}
现在var_dump打印以下内容:
`array(4) {
["id"]=>
string(2) "69"
["type_id"]=>
string(2) "24"
["parent_id"]=>
string(2) "68"
["data"]=>
array(13) {
["object_name"]=>
string(28) "Hajé restaurant de Taveerne"
["object_createdate"]=>
string(10) "1584437106"
["object_createuser"]=>
string(3) "{4}"
["object_read"]=>
string(3) "{4}"
["object_write"]=>
string(3) "{4}"
["object_delete"]=>
string(3) "{4}"
["object_update"]=>
string(3) "{4}"
["object_display"]=>
string(4) "tree"
["object_priority"]=>
string(1) "1"
["data_html"]=>
string(80) "<p>Hajé de Taveerne Oostvaardersdijk 01-09 8244 PA Lelystad Nederland</p>"
["link"]=>
string(162) "https://www.google.com/maps/place/Haj%C3%A9+restaurant+de+Taveerne/@52.522116,5.437504,12z/data=!4m5!3m4!1s0x0:0x2a85d0586811bc85!8m2!3d52.522116!4d5.437504?hl=nl"
["latiude"]=>
array(1) {
[0]=>
string(9) "52.522116"
}
["longtitude"]=>
array(1) {
[0]=>
string(8) "5.437504"
}
}
}`
这很完美,但当我调用index.php中的函数时,它会从数组中删除lat和long:
$locations = $blokken->getLocaties();
var_dump($locations);
var_dump打印以下内容:
`array(1) {
[0]=>
array(4) {
["id"]=>
string(2) "69"
["type_id"]=>
string(2) "24"
["parent_id"]=>
string(2) "68"
["data"]=>
array(11) {
["object_name"]=>
string(28) "Hajé restaurant de Taveerne"
["object_createdate"]=>
string(10) "1584437106"
["object_createuser"]=>
string(3) "{4}"
["object_read"]=>
string(3) "{4}"
["object_write"]=>
string(3) "{4}"
["object_delete"]=>
string(3) "{4}"
["object_update"]=>
string(3) "{4}"
["object_display"]=>
string(4) "tree"
["object_priority"]=>
string(1) "1"
["data_html"]=>
string(80) "<p>Hajé de Taveerne Oostvaardersdijk 01-09 8244 PA Lelystad Nederland</p>"
["link"]=>
string(162) "https://www.google.com/maps/place/Haj%C3%A9+restaurant+de+Taveerne/@52.522116,5.437504,12z/data=!4m5!3m4!1s0x0:0x2a85d0586811bc85!8m2!3d52.522116!4d5.437504?hl=nl"
}
}
}`
我有点困惑,为什么我的指数中没有得到lat和long
在$locations
上迭代时,$location
变量被封闭在循环中,而不是写回$locations
。
尝试添加对$location
的引用
public function getLocaties(){
$locations = $this->getObjects(68);
foreach ($locations as &$location) {
$latAndLong = $this->getLatAndLong($locations ['data']['link']);
$location['data']['latiude'] = $latAndLong['lat'];
$location['data']['longtitude'] = $latAndLong['long'];
var_dump($locatie);
}
return $locaties;
}