Get variable from PHP Session array with Curly Braces

时间:2015-04-29 00:29:20

标签: php json session

I have this command line:

echo $_SESSION['info'];

It outputs this exact array with curly braces included:

{"_var1":"User","_var2":"Password"}

How to get the information from these variables? To output 'User' I've tried:

echo $_SESSION['info']['_var1'];

but doesn't output anything.

1 个答案:

答案 0 :(得分:3)

That's JSON. To get those values you need to use json_decode():

$info = json_decode($_SESSION['info'], true);
echo $info['_var1'];  // User

The above example gives you an array since your question was using them. But you can also get back an object:

$info = json_decode($_SESSION['info']);
echo $info->_var1;  // User