2016-04-20 77 views
-1

我使用GET請求連接到API。當我在JSON數組中使用var_dump時,我得到了完整的數組,並且我只想得到一些像「email」這樣的值。如何在PHP中回顯一個數組的變量?

這是響應:

{ 
    ["communication"]=> array(12) { 
     ["@id"]=> string(8) "84730060" 
     ["@uri"]=> string(63) "https://gethope.fellowshiponeapi.com/v1/Communications/84730060" 
     ["household"]=> array(2) { 
      ["@id"]=> string(8) "46312207" 
      ["@uri"]=> string(59) "https://gethope.fellowshiponeapi.com/v1/Households/46312207" 
     } 
     ["person"]=> array(2) { 
      ["@id"]=> string(8) "75977434" 
      ["@uri"]=> string(55) "https://gethope.fellowshiponeapi.com/v1/People/75977434" 
     } 
     ["communicationType"]=> array(3) { 
      ["@id"]=> string(1) "4" 
      ["@uri"]=> string(75) "https://gethope.fellowshiponeapi.com/v1/Communications/CommunicationTypes/4" 
      ["name"]=> string(5) "Email" 
     } 
     ["communicationGeneralType"]=> string(5) "Email" 
     ["communicationValue"]=> string(19) "[email protected]" 
     ["searchCommunicationValue"]=> string(19) "[email protected]" 
     ["preferred"]=> string(4) "true" 
     ["communicationComment"]=> NULL 
     ["createdDate"]=> string(19) "2015-11-17T13:30:24" 
     ["lastUpdatedDate"]=> string(19) "2016-02-02T14:08:22" 
    } 
} 
+0

請更正你的代碼。你有一條正確的路徑,它不等於echo語句之前的任何東西,並且在你的解釋中有更多的錯誤。 – Webeng

+0

你能清楚你所期望的是什麼嗎? –

回答

0

看起來你已經解碼的JSON響應到一個關聯數組,所以從這一點可以使用值的索引數組中引用特定的值。

所以,如果你想從陣列中獲得communicationValue,那麼你可以這樣做:

// Assuming that $result represents your JSON response 
$response = json_decode($result, true); 

$communicationValue = $response["communication"]["communicationValue"]; 
相關問題