duandai0373
duandai0373
2016-09-12 07:33

使用php从嵌套的Json中获取结果

已采纳

I want to retrieve a value out of a nested JSON file with PHP. I created the following code:

<?php
$string = file_get_contents("https://api.abc");
$json = json_decode($string,true);
foreach ($json['contacts'][0]['properties']['company'] as $person) {

  echo $person['value'];
}
?>

It will only give me 1 result and only the first letter of the value. How do I get more results and more than just the first letter?

{
    "contacts": [{
        "addedAt": 1414182103652,
        "vid": 2,
        "canonical-vid": 2,
        "merged-vids": [],
        "portal-id": 448845,
        "is-contact": true,
        "properties": {
            "firstname": {
                "value": "Andre"
            },
            "lastmodifieddate": {
                "value": "1472024018755"
            },
            "company": {
                "value": "test"
            },
            "lastname": {
                "value": "Jansen"
            }
        },
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doutinghou6980 doutinghou6980 5年前

    try this, use only $person on the place of $person['value']

    $json = json_decode($string,true);
    foreach ($json['contacts'][0]['properties']['company'] as $person) {
      echo $person;
    }
    

    DEMO

    点赞 评论 复制链接分享

相关推荐