如何在PHP中访问嵌套数组对象值?

时间:2018-03-20 09:31:31

标签: php object

我正在使用this unofficial PHP SDK进行Clearbit服务,以丰富身份数据。它采用了Clearbit的API,它本身将结果身份记录作为JSON返回。就像这样......

 require_once '/home/mysite/public_html/path/to/clearbit/vendor/autoload.php';
 use Clearbit\Clearbit;

  $clearbit = Clearbit::create('my_api_key');
  $combined = $clearbit->getCombined('satya.nadella@microsoft.com');

根据图书馆的documentation,它会执行以下任一操作:

$combined->getPerson()(在我的情况下)

$combined->getCompany()

如果我转储像var_dump($combined->getPerson()这样的内容,我看不到JSON,但看起来像数组对象...

Clearbit\Generated\Model\Person::__set_state(array(
   'id' => '1db9c8f3-366c-46a1-8612-213b17da133d',
   'fuzzy' => false,
   'name' => 
  ArrayObject::__set_state(array(
     'fullName' => 'Satya Nadella',
     'givenName' => 'Satya',
     'familyName' => 'Nadella',
  )),
   'gender' => NULL,
   'location' => 'Seattle, WA',
   'timeZone' => NULL,
   'utcOffset' => NULL,
   'geo' => 
  Clearbit\Generated\Model\Geo::__set_state(array(
     'streetNumber' => NULL,
     'streetName' => NULL,
     'subPremise' => NULL,
     'city' => NULL,
     'state' => NULL,
     'stateCode' => NULL,
     'postalCode' => NULL,
     'country' => NULL,
     'countryCode' => NULL,
     'lat' => NULL,
     'lng' => NULL,
  )),
   'bio' => NULL,
   'site' => NULL,
   'avatar' => NULL,
   'employment' => 
  ArrayObject::__set_state(array(
     'domain' => 'microsoft.com',
     'name' => 'Microsoft',
     'title' => 'CEO',
     'role' => 'ceo',
     'seniority' => 'executive',
  )),
   'facebook' => 
  ArrayObject::__set_state(array(
     'handle' => NULL,
  )),
   'github' => 
  ArrayObject::__set_state(array(
     'handle' => NULL,
     'id' => NULL,
     'avatar' => NULL,
     'company' => NULL,
     'blog' => NULL,
     'followers' => NULL,
     'following' => NULL,
  )),
   'twitter' => 
  ArrayObject::__set_state(array(
     'handle' => NULL,
     'id' => NULL,
     'bio' => NULL,
     'followers' => NULL,
     'following' => NULL,
     'statuses' => NULL,
     'favorites' => NULL,
     'location' => NULL,
     'site' => NULL,
     'avatar' => NULL,
  )),
   'linkedin' => 
  ArrayObject::__set_state(array(
     'handle' => 'in/satya-nadella-3145136',
  )),
   'googleplus' => 
  ArrayObject::__set_state(array(
     'handle' => NULL,
  )),
   'angellist' => NULL,
   'aboutme' => 
  ArrayObject::__set_state(array(
     'handle' => NULL,
     'bio' => NULL,
     'avatar' => NULL,
  )),
))

我需要做的是,但不了解如何访问其中一些单独的值 - 例如city内的geo

我已经阅读过各种各样的内容,比如......

  $myperson = $combined->getPerson();
  echo $myperson['gender'];

和...

$mycity = $combined->getPerson()->geo->city;

但是,说实话,我还不知道我在做什么,需要学习一点。

我知道有类似头衔的问题可能已被提及并得到解答,但我的问题本身可能不正确,或者说我的术语不正确。

1 个答案:

答案 0 :(得分:0)

此对象与其他对象结合使用。你应该使用两个对象来检索像城市一样的值。

这样的事情:

$person = $combined->getPerson();
$geo    = $person->getGeo();
$city   = $geo->getCity();
echo $city;

使用 Geo.php 的功能获取Clearbit\Generated\Model\Geo的值

使用 Person.php 的功能获取Clearbit\Generated\Model\Person

的值

参考:

https://github.com/Wisembly/clearbit-php/blob/master/generated/Model/Person.php https://github.com/Wisembly/clearbit-php/blob/master/generated/Model/Geo.php