For each loop quit working.
public static function getMiles($to, $from)
{
$from = urlencode($from);
$to = urlencode($to);
$data = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=$from&destinations=$to&key=MyKey");
$time = 0;
$distance = 0;
/* return as an object for easier notation */
$json=json_decode( $data, false );
$origins = $json->origin_addresses;
$destinations=$json->destination_addresses;
/* if there are multiple rows, use a loop and `$rows[$i]` etc */
$elements=$json->rows[0]->elements;
foreach( $elements as $i => $obj ){
}
//extract the mileage as meters
$miles = $obj->distance->value;
//converting the meters to miles
$distance= round(($miles * '0.000621371192'), 1);
//returning miles
return $distance;
}
The end result is to return mileage between 2 addresses. Error message is "'Invalid argument supplied for foreach()'".
Well, it is my fault. Upon further digging, I was able to determine that my credit card had expired. Reset my expire date and code is working again. Thanks for helping me Muhammad Omer Aslam for pointing me to get the right answer.