0

Hey guys Im a student and my question might seem simple but i can't find the right answer anywhere. Please take a look. as you can see $parsed_json works fine with this code $parsed_json = $parsed_json['photos']; how do i add another key of the array there? i tried $parsed_json = $parsed_json[(['photos']['contactInfo'])]; but did now work. Any help will much appreciated. Thank you.

<?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"
    } ]
  }
}
SHARKY
  • 11
  • 1
  • 2
    Possible duplicate of [PHP - Accessing Multidimensional Array Values](https://stackoverflow.com/questions/17139453/php-accessing-multidimensional-array-values) – Jonathan Kuhn Aug 03 '17 at 23:09
  • Square brackets provide access to elements of the array or string or object(javascript) or JSON. So use $parsed_json[any][amount][of][braces] to reach needed level of parsed JSON – Timur Gilauri Aug 03 '17 at 23:18
  • Please add a sample of your JSON. It's clear to see what's wrong, but without a look at the input, it's not as obvious how to fix it. – Don't Panic Aug 03 '17 at 23:24
  • @Don'tPanic Hey I just added the JSON. Please have a look and let me know. thank you. – SHARKY Aug 04 '17 at 00:13

1 Answers1

0

The short answer is that if $parsed_json represents an associative array of associative arrays, then it stands to reason that $parsed_json['photos'] is an associative array itself. To access the 'contactInfo' value inside it, you simply use $parsed_json['photos']['contactInfo'].

  • the value is not inside it but next to it. please have a look at my JSON. @Aryaman – SHARKY Aug 04 '17 at 00:16
  • @AryamanTummalapalli This is an under-researched duplicate question, please delete your answer and flag to close for any reason you like. – mickmackusa Aug 04 '17 at 04:33