从对象中获取返回的数组值到变量PHP中


Get returned array values from object into variable PHP

我对OOP PHP很陌生,我写了一个类来从谷歌的地图API中获取时间/长度。这有点不相关,因为它工作得很好。

从类返回LAT/LONG/ADDRESS值的数组,如下所示:

    class latLngFinder {
    /**
    * Creates cURL connection to Google API v3
    * @param string $url The URL to read from
    * @return string The URL content
    */
    private function getURL($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        $tmp = curl_exec($ch);
        curl_close($ch);
        if ($tmp != false){
            return $tmp;
        }
    }
    /**
    * Get Latitude/Longitude/Formatted Address based on an address
    * @param string $address The address for finding coordinates
    * @return array An array containing Latitude/Longitude/Formatted Address
    */
    public function findCoord($address){
        $address = str_replace(' ', '+', $address);
        $url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . $address . '&sensor=false';
        $data = $this->getURL($url);
        if ($data){
            $xml = new SimpleXMLElement($data);
            $status = $xml->status;
            // Use this to debug returned XML
            //print_r($xml);
            if ($status == 'OK'){
                // Find Latitude/Longitude/Formatted Address in XML
                $lat = array( (string) $xml->result->geometry->location->lat);
                $lng = array( (string) $xml->result->geometry->location->lng);
                $formatted_addr = array( (string) $xml->result->formatted_address);
            }
        }
        // Return data
        return array('formatted_address' =>  $formatted_addr[0], 'lat' => $lat[0], 'lng' => $lng[0]);
    }

};

当我创建类对象时:

 // Include class
    require_once(dirname(__FILE__) . '/class.latLngFinder.php');
    // Initialise the latLngFinder class
    $obj = new latLngFinder();
    print_r($obj->findCoord($row['address']));

我想获得从类返回的数组,这就是我卡住的地方。我试过使用:

$var = print_r($obj->findCoord($row['address']), true);

然而,这返回数组作为一个字符串,我不希望。有人能帮忙吗?

谢谢。

<?php
class latLngFinder {
    public $output;
    public $lat;
    public $lng;
    public $formatted_address;
    public function __construct($inputaddress)
    {
        $this->address = $inputaddress;
        $this->output = $this->findCoord();
    }
    /**
    * Creates cURL connection to Google API v3
    * @param string $url The URL to read from
    * @return string The URL content
    */
    private function getURL($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        $tmp = curl_exec($ch);
        curl_close($ch);
        if ($tmp != false){
            return $tmp;
        }
    }
    /**
    * Get Latitude/Longitude/Formatted Address based on an address
    * @param string $address The address for finding coordinates
    * @return array An array containing Latitude/Longitude/Formatted Address
    */
    private function findCoord(){
        $address = str_replace(' ', '+', $this->address);
        $url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . $address . '&sensor=false';
        $data = $this->getURL($url);
        if ($data){
            $xml = new SimpleXMLElement($data);
            $status = $xml->status;
            // Use this to debug returned XML
            //print_r($xml);
            if ($status == 'OK'){
                // Find Latitude/Longitude/Formatted Address in XML
                $lat = $xml->result->geometry->location->lat;
                $lng = $xml->result->geometry->location->lng;
                $formatted_addr = $xml->result->formatted_address;
                $this->lat = $lat;
                $this->lng = $lng;
                $this->formatted_address = $formatted_addr;
            }
        }
    }
}
// Initialise the latLngFinder class
$obj = new latLngFinder($row['address']);
echo $obj->lat;
echo "<br />";
echo $obj->lng;
echo "<br />";
echo $obj->formatted_address;
?>