无法在多维数组中回显字符串但可以print_r

时间:2017-04-18 14:18:03

标签: php arrays multidimensional-array

我遇到了一个奇怪的问题,我可以print_r()我的变量,它会产生以下结果:

print_r($_SESSION['userdata']);

outputs: Array ( [0] => Array ( [id] => 1 [username] => admin [firstname] => carl [lastname] => wirkus [password] => password [admin] => 1 [staff] => 0 [tenant] => 0 [email] => test@test.com ) 

但是,当我尝试用以下内容回显用户名时:

echo "You are currently logged in as: " . $_SESSION['userdata']['username'] . "<br>";

outputs: You are currently logged in as: 

我做错了什么?

1 个答案:

答案 0 :(得分:5)

仔细查看print_r($_SESSION['userdata']);。它是数组中的数组。所以你不够深入到达你的价值观。

echo "You are currently logged in as: " . $_SESSION['userdata'][0]['username'] . "<br>";