簡體   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