I am trying get coordinate from addresses, but having an error which is called undefined offset. Actually array is not null and when I try to get coordinate one by one, I can get grab it without a problem. But when I try get all at once error pops up.
$addr = [];
foreach($newHtml as $link){
$htm = pageContent($link);
$paths = new \DOMXPath($htm);
$routes = $paths->query("//body/div[@class='p-main']//table/tr[4]/td");
foreach ($routes as $route) {
$addr = trim($route->nodeValue);
}
$address = urlencode($addr);
$response = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=MY_API_KEY");
$result = json_decode($response);
$lat[] = trim($result->results[0]->geometry->location->lat);
$lng[] = trim($result->results[0]->geometry->location->lng);
}
var_dump($lat, $lng);
Response gives this;
["results"]=> array(1) { [0]=> object(stdClass)#696 (6) { ["address_components"]=> array(8) { [0]=> object(stdClass)#684 (3) { ["long_name"]=> string(9) "D21" ["short_name"]=> string(9) "D21" ["types"]=> array(1) { [0]=> string(7) "premise" } } [1]=> object(stdClass)#709 (3) { ["long_name"]=> string(1) "1" ["short_name"]=> string(1) "1" ["types"]=> array(3) { [0]=> string(9) "political" [1]=> string(11) "sublocality" [2]=> string(19) "sublocality_level_4" } } [2]=> object(stdClass)#714 (3) { ["long_name"]=> string(7) "5 Chome" ["short_name"]=> string(7) "5 Chome" ["types"]=> array(3) { [0]=> string(9) "political" [1]=> string(11) "sublocality" [2]=> string(19) "sublocality_level_3" } } [3]=> object(stdClass)#698 (3) { ["long_name"]=> string(8) "Furuedai" ["short_name"]=> string(8) "Furuedai" ["types"]=> array(3) { [0]=> string(9) "political" [1]=> string(11) "sublocality" [2]=> string(19) "sublocality_level_2" } } [4]=> object(stdClass)#680 (3) { ["long_name"]=> string(5) "Suita" ["short_name"]=> string(5) "Suita" ["types"]=> array(2) { [0]=> string(8) "locality" [1]=> string(9) "political" } } [5]=> object(stdClass)#701 (3) { ["long_name"]=> string(16) "Osaka Prefecture" ["short_name"]=> string(16) "Osaka Prefecture" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_1" [1]=> string(9) "political" } } [6]=> object(stdClass)#695 (3) { ["long_name"]=> string(5) "Japan" ["short_name"]=> string(2) "JP" ["types"]=> array(2) { [0]=> string(7) "country" [1]=> string(9) "political" } } [7]=> object(stdClass)#699 (3) { ["long_name"]=> string(8) "565-0874" ["short_name"]=> string(8) "565-0874" ["types"]=> array(1) { [0]=> string(11) "postal_code" } } } ["formatted_address"]=> string(69) "5 Chome-1-D21 Furuedai, Suita, Osaka Prefecture 565-0874, Japan" ["geometry"]=> object(stdClass)#690 (3) { ["location"]=> object(stdClass)#694 (2) { ["lat"]=> float(34.8097093) ["lng"]=> float(135.5119112) } ["location_type"]=> string(7) "ROOFTOP" ["viewport"]=> object(stdClass)#689 (2) { ["northeast"]=> object(stdClass)#688 (2) { ["lat"]=> float(34.811058280291) ["lng"]=> float(135.51326018029) } ["southwest"]=> object(stdClass)#692 (2) { ["lat"]=> float(34.808360319709) ["lng"]=> float(135.51056221971) } } } ["place_id"]=> string(27) "ChIJUY4O7kP7AGARu_UB4lJPFWU" ["plus_code"]=> object(stdClass)#687 (2) { ["compound_code"]=> string(30) "RG56+VQ Suita, 大阪府 Japan" ["global_code"]=> string(11) "8Q6QRG56+VQ" } ["types"]=> array(2) { [0]=> string(13) "establishment" [1]=> string(17) "point_of_interest" } } } ["status"]=> string(2) "OK"