2016-04-20 84 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"]; 
相关问题