Get the value of Resultcode from a decoded json array

时间:2021-01-26 13:42:20

Hi guys i know this could be easy but i have tried and still come up short. I want to get the value of the result code but still not able to print it so i can do an if statement

嗨伙计们,我知道这可能很容易,但我已经尝试过,但仍然很短。我想获得结果代码的值,但仍然无法打印它,所以我可以做一个if语句

//loop through json array 

$str = file_get_contents('Response.json');
$array = json_decode($str, true);

$last = array_pop($array);

echo "<pre>";
echo $last[0];

//echo $last[0]['Body']['stkCallback']['ResultCode'];

The line echo $last[0] works and gives me

行echo $ last [0]可以工作并给我

{"Body":{"stkCallback":{"MerchantRequestID":"16421-6174532-2","CheckoutRequestID":"ws_CO_DMZ_99605283_17102018133825256","ResultCode":1036,"ResultDesc":"[STK_CB - ]SMSC ACK timeout."}}}

But

$last[0]['Body']['stkCallback]['ResultCode'] 

doesn't echo or print out anything just a {

不会回显或打印任何内容{

Thanks

3 个解决方案

#1


0  

It's because that you are still getting a json from this line. You need to decode it as well:

这是因为你仍然从这一行得到一个json。你还需要解码它:

print_r(json_decode($last[0],true));

This should return you the array you need.

这应该返回你需要的数组。

Remember that the fact that you can echo it means that it is still in json format otherwise you would get an error.

请记住,您可以回显它的事实意味着它仍然是json格式,否则您将收到错误。

#2


0  

Seems like when you echo $last[0] you are seeing another json string, not an object. You will need to decode that too:

好像当你回显$ last [0]时,你会看到另一个json字符串,而不是一个对象。您还需要解码它:

$last = json_decode(array_pop($array), true);
print_r($last);

#3


0  

It seems like some of your array value exists json string, if so, this may be a solution

看起来你的某些数组值存在json字符串,如果是这样,这可能是一个解决方案

#1


0  

It's because that you are still getting a json from this line. You need to decode it as well:

这是因为你仍然从这一行得到一个json。你还需要解码它:

print_r(json_decode($last[0],true));

This should return you the array you need.

这应该返回你需要的数组。

Remember that the fact that you can echo it means that it is still in json format otherwise you would get an error.

请记住,您可以回显它的事实意味着它仍然是json格式,否则您将收到错误。

#2


0  

Seems like when you echo $last[0] you are seeing another json string, not an object. You will need to decode that too:

好像当你回显$ last [0]时,你会看到另一个json字符串,而不是一个对象。您还需要解码它:

$last = json_decode(array_pop($array), true);
print_r($last);

#3


0  

It seems like some of your array value exists json string, if so, this may be a solution

看起来你的某些数组值存在json字符串,如果是这样,这可能是一个解决方案