0

Have tried every variation I can think of to access an object property in an array. I'm getting some data back form an API which i'm storing in a variable called $userTokenValid:

$userTokenValid = [{"authTokenValid":1}];

i'm then trying to access the authTokenValid property like so:

echo json_decode($userTokenValid[0]->authTokenValid);

I appreciate this might be quite basic but can't spot where I have gone wrong.

red house 87
  • 1,837
  • 9
  • 50
  • 99

1 Answers1

2

$userTokenValid isn't valid php. However [{"authTokenValid":1}] is a valid json string.

$userTokenValid = '[{"authTokenValid":1}]';

you can decode it with

$json = json_decode($userTokenValid);

finally

echo $json[0]->authTokenValid;
Federkun
  • 36,084
  • 8
  • 78
  • 90