我如何在PHP中打印对象数组的单个元素?

时间:2014-06-09 23:27:14

标签: php google-maps object

我找到了使用Google API将地址转换为GPS经度/纬度的课程。

我可以打印所有数组:print_r($geo->adress(my address)

会显示

Array
(
    [0] => stdClass Object
        (
            [location] => stdClass Object
                (
                    [lat] => 53.1243946
                    [lng] => 18.001958
                )

            [city] => Bydgoszcz
            [street] => MarszaĹka Focha
            [number] => 4
            [code] => 85-070
        )

)

但我不能打印单个元素。我需要添加位置 - > lat / lng到变量,但我无法将对象转换为字符串。我怎么能这样做?

$lat = implode(",", $geo->adress('Focha 4, Bydgoszcz'); // doesn't work at all. 

<?php

echo "<pre>";
$geo = new GeoGoogle;
print_r($geo->adress('Focha 4, Bydgoszcz'));
echo"</pre>";



final class GeoGoogle {

  // zwróć listę lokacji pasujących do podanego adresu
  function adress($adress) {
    return $this->parse(
      $this->prepare(
        'http://maps.googleapis.com/maps/api/geocode/json?address='
        .urlencode($adress)
        .'&sensor=false'
      )
    );
  }

  // zwróć listę lokacji pasujących do podanej długości i szerokości
  function latlng($lat,$lng) {
    return $this->parse(
      $this->prepare(
        'http://maps.googleapis.com/maps/api/geocode/json?latlng='
        .$lat .','. $lng
        .'&sensor=false'
      )
    );
  }

  /* pomocnicze */

  private function prepare($uri) {
    return json_decode( file_get_contents($uri) );
  }

  private function parse($data) {
    if($data->status == 'OK') {
      $results = array();
      foreach($data->results as $res) { // przetwórz wyniki
        $result = array(
          'location' => null,
          'city' => null,
          'street' => null,
          'number' => null,
          'code' => null
        );
        // pobierz współrzędne
        if(isset($res->geometry)) {
          if(isset($res->geometry->location)) {
            $result['location'] = $res->geometry->location;
          }
        }
        // pobierz dane
        foreach($res->address_components as $component) {
          foreach($component->types as $type) {
            switch($type) {
              case 'street_number':
                $result['number'] = $component->long_name;
                break;
              case 'route':
                $result['street'] = $component->long_name;
                break;
              case 'postal_code':
                $result['code'] = $component->long_name;
                break;
              case 'sublocality':
                $result['city'] = $component->long_name;
                break;
              case 'administrative_area_level_3':
                if(is_null($result['city'])) {
                  $result['city'] = $component->long_name;
                }
                break;
            }
          }
        }
        if(!is_null($result['city'])) {
          $results[] = (object) $result;
        }
      }
      return $results;
    }
    return array();
  }
}

1 个答案:

答案 0 :(得分:3)

获得&#39; lat&#39;和&#39; lng&#39;你的对象的价值,试试这个:

$lat = $object->location['lat'];
$lng = $object->location['lng'];

See demo

编辑:仔细观察您的数据,这可能会更有帮助:

$lat = $geo[0]->location['lat'];
$lng = $geo[0]->location['lng'];

See demo 2

编辑:关于评论Cannot use object of type GeoGoogle as array

中所述的错误

试试这个:

$my_geo = $geo->adress(my address);

$lat = $my_geo[0]->location->lat;
$lng = $my_geo[0]->location->lng;