Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Is there any method to parse json nested object in php

–2 votes
asked Jun 30, 2019 by Priyanka
hello, im making a site and trying to read real time data from the api of that site. is there any method by which i can parse nested json objects in my php file.

i had tried doing the same by this  
   function get_json($url)
   {
      $ch = curl_init();

      //Very short time to get json so long timeout not needed
       $timeout = 5;
       curl_setopt($ch,CURLOPT_URL,$url);
       curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
       curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
       $data = curl_exec($ch);
       curl_close($ch);
       return $data;
   }

   $json = get_json($url);

   $info = json_decode($json,true);

//Prints the outer variables
print_r($info);

my json file is {
BTC_BCN: {
BTC: "2.00323120",
BCN: "24494034.10065716"
},
BTC_BTS: {
BTC: "5.18214945",
BTS: "923580.27820923"
},
BTC_CLAM: {
BTC: "0.74366300",
CLAM: "2359.13859911"
},
BTC_DASH: {
BTC: "46.53285453",
DASH: "3315.34208624"
},
BTC_DGB: {
BTC: "25.82338089",
DGB: "25287604.56472447"
},
BTC_DOGE: {
BTC: "28.00194195",
DOGE: "99059918.76344815"
}}

i want the output to be 2.00499225 btc = 24513601.32287938 bcn but im getting => Array ( [BTC_BCN] => Array ( [BTC] => 2.00499225 [BCN] => 24513601.32287938 ) [BTC_BTS] => Array ( [BTC] => 5.27151912 [BTS] => 939529.28454899 )

1 Answer

0 votes
answered Jul 16, 2019 by Abhivirusonline (180 points)

use the dot (.) operator to access the required data object you want to access.

for your required output :

$info.BTC_BCN.BTC that will give you 2.00499225

similarly you can do whatever you wanted do.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...