简体   繁体   English

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

[英]How do I access nested array object values in PHP?

I am using this unofficial PHP SDK for the Clearbit service, to enrich identity data. 我将这个非正式的PHP SDK用于Clearbit服务,以丰富身份数据。 It taps in to Clearbit's API, which natively returns resulting identity records as JSON. 它使用Clearbit的API,该API会以本地方式将生成的身份记录返回为JSON。 It goes like this... 像这样...

 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');

Per the library's documentation , it then performs either: 根据库的文档 ,它然后执行以下任一操作:

$combined->getPerson() (in my case) $combined->getPerson() (以我$combined->getPerson()

or 要么

$combined->getCompany()

If I dump the contents like var_dump($combined->getPerson() , I see not JSON but what looks to me like an array object... 如果我转储类似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,
  )),
))

What I need to do, but don't understand how to, is access some of those individual values - like the city that is inside geo , for example. 我需要做的,但不了解如何做的,是访问其中的一些单独的值-例如,位于geo内部的city

I have read around and tried various, like... 我已经阅读并尝试了各种方法,例如...

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

and... 和...

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

But, truth be told, I don't yet know what I'm doing, and need to learn a bit. 但是,说实话,我还不知道我在做什么,需要学习一些。

I'm aware that questions with similar titles have probably been asked and answered, but it's possible my question itself is not even correct or that I don't have my terminology correct. 我知道可能已经提出并回答了标题相似的问题,但是我的问题本身甚至可能不正确,或者我的术语不正确。

this object is combined with other objects. 该对象与其他对象结合在一起。 you should use both objects to retrieve the value like the city. 您应该同时使用两个对象来检索城市值。

Something like this: 像这样:

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

Use functions of Geo.php to get values of Clearbit\\Generated\\Model\\Geo 使用Geo.php函数获取Clearbit\\Generated\\Model\\Geo

Use functions of Person.php to get values of Clearbit\\Generated\\Model\\Person 使用Person.php函数获取Clearbit\\Generated\\Model\\Person

Reference: 参考:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM