简体   繁体   English

将json分支数组转换为php对象

[英]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. 在下面的代码中,我想使用echo显示get_data但无法解码/显示分支数组。 So, My question is how to convert branched array to PHP object. 所以,我的问题是如何将分支数组转换为PHP对象。

Json Response : Json回应:

{"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) PHP代码:(使用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. echo User_data不起作用。

So you want to get value of get_data . 因此,您想获取get_data值。 If d91c2d21af80002a3dd6ffc76f62bb9f89b6e0ba is not known, try this way. 如果不知道d91c2d21af80002a3dd6ffc76f62bb9f89b6e0ba ,请尝试这种方式。

$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. 使用foreach循环,您不必查找索引,并且可以轻松获取值。

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"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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