dongni1892 2018-05-06 04:25
浏览 74

如果值是逗号,php重复函数10次

I'm trying to repeat the function only if the value is ",":

This is my code for trying to get the coordinates from an address but somtimes it gets only "," so I want it to try 10 times until it gets the full coordinates.

$coordinates1 = getCoordinates($placeadress); 
$i == 0;
while (($coordinates1 == ',') && ($i <= 10)) {
    $coordinates1 = getCoordinates($placeadress);
    $i++;
}

The function code is this:

function getCoordinates($address) {
    $address = str_replace(" ", "+", $address); // replace all the white space with "+" sign to match with google search pattern
    $address = str_replace("-", "+", $address); // replace all the "-"  with "+" sign to match with google search pattern
    $url = "http://maps.google.com/maps/api/geocode/json?address=$address";
    $response = file_get_contents($url);
    $json = json_decode($response,TRUE); //generate array object from the response from the web
    return ($json['results'][0]['geometry']['location']['lat'].",".$json['results'][0]['geometry']['location']['lng']);
}   
  • 写回答

2条回答 默认 最新

  • doufuxing8562 2018-05-06 05:06
    关注

    May be try this

    $coordinates1 = getCoordinates($placeadress); 
    $i = 0;
        while ($i <= 10) {
        $coordinates1 = getCoordinates($placeadress);
        if($coordinates1==',')
            $i++;
        else
            break;
        }
    

    It will break the loop as soon as co-ordinates value is not a comma and you are good to go. If it is a comma it will go for next iteration in while

    评论

报告相同问题?