如何在PHP中回显数组的一个变量?

时间:2016-04-20 21:18:00

标签: php arrays string api rest

我使用GET请求连接到API。当我在JSON中var_dump数组时,我得到了完整的数组,我只想获得一些像" email"这样的值。

这是回复:

{
    ["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) "geroges@gethope.net"
        ["searchCommunicationValue"]=> string(19) "geroges@gethope.net"
        ["preferred"]=> string(4) "true"
        ["communicationComment"]=> NULL
        ["createdDate"]=> string(19) "2015-11-17T13:30:24"
        ["lastUpdatedDate"]=> string(19) "2016-02-02T14:08:22"
    }
}

1 个答案:

答案 0 :(得分:0)

看起来您已经将JSON响应解码为关联数组,因此从那时起您可以使用值的索引引用数组中的特定值。

因此,如果您想从数组中获取communicationValue,那么您可以这样做:

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

$communicationValue = $response["communication"]["communicationValue"];