如何解析多关联数组以按键值回显

时间:2017-08-03 23:05:13

标签: php json api

嘿伙计我是学生,我的问题可能看似简单,但我无法在任何地方找到正确的答案。请看一下。你可以看到$ parsed_json可以正常使用这段代码$parsed_json = $parsed_json['photos'];如何在那里添加数组的另一个键?我试过$parsed_json = $parsed_json[(['photos']['contactInfo'])];,但现在确实有效。任何帮助将非常感谢。谢谢。

<?php

$json_string =    file_get_contents("api/goes/here")
$parsed_json = json_decode($json_string, true);

print_r($parsed_json);

$parsed_json = $parsed_json[(['photos']['contactInfo'])];



foreach($parsed_json as $key => $value)
{
echo $value['typeName'] . '<br>';
echo $value['typeId'] . '<br>';
echo $value['familyName'] . '<br>';
// etc


?>

------------- JSON CONTENT ------------------------------

{
  "status" : 200,
  "requestId" : "0ef05ee4-6bab-4488-a80c-04ce050ca074",
  "likelihood" : 0.95,
  "photos" : [ {
    "type" : "linkedin",
    "typeId" : "linkedin",
    "typeName" : "LinkedIn",
    "url" : "https://d2ojpxxtu63wzl.cloudfront.net/static/651f183b3a1bd463ef5298a2877f2c4f_0c027087db5cd03d244a7dd42e42fc969fa8d636bffd7635b46172c8a597aa73",
    "isPrimary" : true
  }, {
    "type" : "twitter",
    "typeId" : "twitter",
    "typeName" : "Twitter",
    "url" : "https://d2ojpxxtu63wzl.cloudfront.net/static/d0080f4ccd9ea21340b0fd994f82c0e0_2acdc7de41111f7d58e7544ed970b78e72f1739784417c146d5689d96ce79137"
  } ],
  "contactInfo" : {
    "familyName" : "Cassini",
    "fullName" : "Flavio Cassini",
    "givenName" : "Flavio"
  },
  "organizations" : [ {
    "name" : "DEVTECH .LLC",
    "startDate" : "2015-03",
    "title" : "President/Owner",
    "current" : true
  } ],
  "demographics" : {
    "locationDeduced" : {
      "normalizedLocation" : "Boca Raton, United States",
      "deducedLocation" : "Boca Raton, Florida, United States",
      "city" : {
        "name" : "Boca Raton"
      },
      "state" : {
        "deduced" : true,
        "name" : "Florida",
        "code" : "FL"
      },
      "country" : {
        "name" : "United States",
        "code" : "US"
      },
      "continent" : {
        "deduced" : true,
        "name" : "North America"
      },
      "county" : {
        "deduced" : true,
        "name" : "Palm Beach"
      },
      "likelihood" : 1.0
    },
    "gender" : "Male",
    "locationGeneral" : "Boca Raton, Florida, United States"
  },
  "socialProfiles" : [ {
    "type" : "klout",
    "typeId" : "klout",
    "typeName" : "Klout",
    "url" : "http://klout.com/DevTechServices",
    "username" : "DevTechServices",
    "id" : "91760869707035580"
  }, {
    "followers" : 67,
    "following" : 67,
    "type" : "linkedin",
    "typeId" : "linkedin",
    "typeName" : "LinkedIn",
    "url" : "https://www.linkedin.com/in/devtechservices",
    "username" : "devtechservices",
    "id" : "428935547"
  }, {
    "bio" : "Student at Full Sail University",
    "followers" : 5,
    "following" : 36,
    "type" : "twitter",
    "typeId" : "twitter",
    "typeName" : "Twitter",
    "url" : "https://twitter.com/DevTechServices",
    "username" : "DevTechServices",
    "id" : "3245620291"
  } ],
  "digitalFootprint" : {
    "scores" : [ {
      "provider" : "klout",
      "type" : "general",
      "value" : 48
    } ],
    "topics" : [ {
      "provider" : "klout",
      "value" : "Bitcoin"
    }, {
      "provider" : "klout",
      "value" : "Content Marketing"
    }, {
      "provider" : "klout",
      "value" : "SAAS"
    }, {
      "provider" : "klout",
      "value" : "Software"
    }, {
      "provider" : "klout",
      "value" : "WordPress"
    } ]
  }
}

1 个答案:

答案 0 :(得分:0)

简短的回答是,如果$parsed_json表示关联数组的关联数组,那么$parsed_json['photos']本身就是一个关联数组。要访问其中的'contactInfo'值,只需使用$parsed_json['photos']['contactInfo']