繁体   English   中英

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

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

我将这个非正式的PHP SDK用于Clearbit服务,以丰富身份数据。 它使用Clearbit的API,该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');

根据库的文档 ,它然后执行以下任一操作:

$combined->getPerson() (以我$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,
  )),
))

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

我已经阅读并尝试了各种方法,例如...

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

和...

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

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

我知道可能已经提出并回答了标题相似的问题,但是我的问题本身甚至可能不正确,或者我的术语不正确。

该对象与其他对象结合在一起。 您应该同时使用两个对象来检索城市值。

像这样:

$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/generation/Model/Geo.php

暂无
暂无

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

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