简体   繁体   中英

converting json branched array to php object

In below code, I want to display get_data using echo but not able to decode/display branched array. So, My question is how to convert branched array to PHP object.

Json Response :

{"status":1,"msg":"fetched Succesfully","user_data":{"d91c2d21af80002a3dd6ffc76f62bb9f89b6e0ba":{"name":"PRATYUSH","user_year":"2013","get_data":"d91c2d21af80002a3dd6ffc76f62bb9f89b6e0ba","is_active":1,"data_no":"ghjgjj2XXXXXXoioo7","user_bin":"77","is_exp":"N"}}}

PHP CODE : (after using curl)

$response = json_decode($o,true);
$status=$response["status"];
$msg=$response["msg"];
$user_data=$response["user_data"][0]["get_data"];

RESULT:

echo $status;//(working)
echo "<br>";
echo $msg;//(working)
echo "<br>";
echo $user_data;//(Not working)

echo User_data is not working.

So you want to get value of get_data . If d91c2d21af80002a3dd6ffc76f62bb9f89b6e0ba is not known, try this way.

$user_data_arr=$response["user_data"]; 
foreach($user_data_arr AS $user_data_obj)
{
    echo $user_data_obj['get_data'];// here is your desired value
}

Using foreach loop, you do not have to find index and you can get values easily.

Full Code

 $response = json_decode($o,true);
 $status=$response["status"];
 $msg=$response["msg"];
 $user_data="";
 $user_data_arr=$response["user_data"]; 
  foreach($user_data_arr AS $user_data_obj)
  {
    $user_data = $user_data_obj['get_data'];// here is your desired value
  }
 echo $status;
 echo "<br>";
 echo $msg;
 echo "<br>";
 echo $user_data;//will work

Change

$user_data=$response["user_data"][0]["get_data"];

to this

$user_data=$response["user_data"]["d91c2d21af80002a3dd6ffc76f62bb9f89b6e0ba"]["get_data"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM