0

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.

6
  • What is the decoded json data in $response? Commented Jan 21, 2016 at 5:37
  • Its a 'array'........ Commented Jan 21, 2016 at 5:50
  • 1
    I mean to ask, did you get your result in such a way?? > Array( ["status"] => '1', ["msg"] => "fetched Succesfully", ["user_data"] => Array( ["d91c2d21af80002a3dd6ffc76f62bb9f89b6e0ba"] => Array( ["name"] => "PRATYUSH", ["user_year"] => "2013", ["get_data"] => "d91c2d21af80002a3dd6ffc76f62bb9f89b6e0ba", ["is_active"] => 1, ["data_no"] => "ghjgjj2XXXXXXoioo7", ["user_bin"] => "77", ["is_exp"] => "N" ) ) ) Commented Jan 21, 2016 at 5:53
  • @abhishek-Thanks, its working. Commented Jan 21, 2016 at 6:47
  • @abhishek What to do when there is more than one get_data in at json response? Like below {"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"},"ty6c2d21af80002a3dd6ffc76f62bb9f89b6e0ba":{"name":"PghfghH","user_year":"2015","get_data":"ty6c2d21af80002a3dd6ffc76f62bb9f89b6e0ba","is_active":1,"data_no":"ghjgjj2XXXXXXoioo7","user_bin":"77","is_exp":"N"}}} Commented Jan 21, 2016 at 7:10

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

Change

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

to this

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

1 Comment

"d91c2d21af80002a3dd6ffc76f62bb9f89b6e0ba" comes in json response part, I can't use this value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.