Keith D Kaiser
Keith D Kaiser

Reputation: 1036

How do I retrieve an array value created in a PHP function?

I have the following in PHP function;

    function geocode($address){
    $address = urlencode($address);
    $url = "http://maps.google.com/maps/api/geocode/json?address={$address}";
    $resp_json = file_get_contents($url);
    $resp = json_decode($resp_json, true);
    if($resp['status']=='OK'){
        $lati = $resp['results'][0]['geometry']['location']['lat'];
        $longi = $resp['results'][0]['geometry']['location']['lng'];
        $formatted_address = $resp['results'][0]['formatted_address'];
        if($lati && $longi && $formatted_address){

            // put the data in the array
            $koords = array();            

            array_push(
                $koords, 
                $lati, 
                $longi, 
                $formatted_address
                );
             //print_r($koords);   //Works fine here
            return $koords;             
        }else{
            return false;
        }

    }else{
        return false;
    }
}

$results = print_r(geocode('some address');  // shows me the output below

Array
(
    [0] => 39.0098762
    [1] => -94.4912607
)

But I don't know how to create a variable outside the function that contains the latitude and longitude. Like this;

$lat = $koords[0];   // does NOT work
$lat = $array[0];  // does NOT work

What (really simple) thing am I forgetting here? How do I set another PHP variable to each of the two elements of the array?

Upvotes: 2

Views: 50

Answers (2)

Kausha Mehta
Kausha Mehta

Reputation: 2928

Simply you can try the below code...

<?php
function geocode($address){
    $address = urlencode($address);
    $url = "http://maps.google.com/maps/api/geocode/json?address={$address}";
    $resp_json = file_get_contents($url);
    $resp = json_decode($resp_json, true);
    if($resp['status']=='OK'){
        $lati = $resp['results'][0]['geometry']['location']['lat'];
        $longi = $resp['results'][0]['geometry']['location']['lng'];
        $formatted_address = $resp['results'][0]['formatted_address'];
        if($lati && $longi && $formatted_address){

            // put the data in the array
            $koords = array();            

            array_push(
                $koords, 
                $lati, 
                $longi, 
                $formatted_address
                );
             //print_r($koords);   //Works fine here
            return $koords;             
        }else{
            return false;
        }

    }else{
        return false;
    }
}

$results = geocode('some address');
//        ^You have to remove print_r() from here otherwise all things are alright
print_r($results);

// You may need to use $results[0], $results[1], etc respectively to getting result
echo $results[0];
echo $results[1];
echo $results[2];
?>

Upvotes: 1

Poiz
Poiz

Reputation: 7617

Since $results contains Some Data (Array-Type Data), You could effectively do something like this instead:

<?php

    // ASSIGN THE DATA RETURNED BY CALLING THE `geocode()` FUNCTION
    // TO A VARIABLE (HERE: $results)
    $results   = geocode('some address');  //<== NOTICE: "NO print_r() HERE!" 


    // CREATE 2 VARIABLES TO HOLD YOUR LATITUDE & LONGITUDE
    // INITIALIZE THESE VARIABLES TO NULL
    $lat       = null;
    $long      = null;


    // TO EXTRACT THE LATITUDE & LONGITUDE VALUES
    // STORED IN THE NUMERICALLY INDEXED ARRAY: $results,
    // 1ST: CHECK IF $results CONTAINS SOME DATA:

    if(!empty($results)){
        // IF IT DOES; ASSIGN VALUES TO $lat & $long RESPECTIVELY.
        $lat   = $results[0];   //<== ASSUMES THE LATITUDE VALUE  IS STORED @INDEX 0
        $long  = $results[1];   //<== ASSUMES THE LONGITUDE VALUE IS STORED @INDEX 1
    }

    // NOW TRY TO SEE THE RESULT OF THE ACTIONS ABOVE:
    var_dump($results); 
    var_dump($lat); 
    var_dump($long); 

Upvotes: 0

Related Questions