如何访问json属性和元素

时间:2018-04-04 20:04:43

标签: php json

的var_dump($响应);

返回: array(6){[“object”] => string(4)“list”[“hasMore”] => bool(false)[“totalCount”] => int(1)[“limit”] => int(10)[“offset”] => int(0)[“data”] => array(1){[0] => array(25){[“object”] => string(8)“customer”[“id”] => string(16)“cus_000004078459”[“dateCreated”] => string(10)“2018-04-04”[“name”] => string(19)“sylvio de lima neto”[“email”] => string(18)“dmsylvio@gmail.com”[“company”] => NULL [“phone”] => NULL [“mobilePhone”] => string(11)“61996441895”[“address”] => string(23)“AvenidadasAraucárias”[“addressNumber”] => string(4)“1735”[“complement”] => NULL [“省”] => string(19)“Sul(ÁguasClaras)”[“postalCode”] => string(8)“71936250”[“cpfCnpj”] => string(11)“05452407197”[“personType”] => NULL [“已删除”] => bool(false)[“additionalEmails”] => NULL [“externalReference”] => NULL [“notificationDisabled”] => bool(false)[“canDelete”] => bool(true)[“canEdit”] => bool(true)[“city”] => int(15872)[“state”] => string(2)“DF”[“country”] => string(6)“Brasil”[“foreignCustomer”] => bool(false)}}}

如何进行: [ “名称”] => string(19)“sylvio de lima neto”

示例:

<?php if (isset($response)): ?>
<?php $json = json_decode($response, true); ?>
<?php if ($json['name']): ?>
    <p>User: <?php echo $json['name']; ?></p>
<?php endif; ?>

1 个答案:

答案 0 :(得分:1)

name键位于data数组内,位于第一个元素中。您应该使用$json['data'][0]['name']

<?php if (isset($response)): ?>
<?php $json = json_decode($response, true); ?>
<?php if (isset($json['data'][0]['name'])): ?>
    <p>User: <?php echo $json['data'][0]['name']; ?></p>
<?php endif; ?>

将输出:

User: sylvio de lima neto