-1

I need to be able to read the number of apiaccountview_id inside the array of this response:

{
"errorCodes": null,
"result": {
    "apiaccountview_id": "553631",
    "apiaccountview_countryId": "UA",
    "apiaccountview_registrationIp": "531137938",
    "apiaccountview_phoneNumber": "12344512344"
},
"success": true

}

I am using

$obj = json_decode($response,true);

And I want to have this to show the apiaccountview_id

if ($obj['success'] =='true'){
  return array(
    'error' => 0,
      'msg' => "added successfully - " . $obj['result=>apiaccountview_id'] . ":" . $obj['result=>apiaccountview_id']
  );
}

else{
    return array(
      'error' => 1,
      'msg' => "Error in loading - " . $obj['errorCodes'] . ":" . $obj['result']
    );
    }

Since result is an array I was thinking on calling the value inside like this:

$obj['result=>apiaccountview_id']

But it didn't work. Can you please help me understand why? How can I get a value inside an json array?

Thank you, Arye

Arye Guetta
  • 45
  • 1
  • 9

1 Answers1

3

You're passing true as second argument to json_decode which means that you will have associative array in your result. You can access apiaccountview_id like this:

echo $obj['result']['apiaccountview_id'];

If you want to return object instead of array, you should pass false (that's default value) and then you can access the property like this:

echo $obj->result->apiaccountview_id;
Kristian Vitozev
  • 5,791
  • 6
  • 36
  • 56
  • Thank you - that worked great. I have seen the answers on "How do I extract data from JSON with PHP?" "How can I access an array/object?" and I could not put my finger on the exact way to read the value. I am sorry if it looks like I was asking the same question. Thank you. – Arye Guetta Oct 07 '15 at 09:45