duanpuqi9965
2017-04-18 14:18
浏览 48
已采纳

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

I've run into a weird problem, I can print_r() my variable and it produces the following:

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 ) 

However when I try to echo the username with:

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

outputs: You are currently logged in as: 

What have I done wrong?

图片转代码服务由CSDN问答提供 功能建议

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

  print_r($ _ SESSION ['userdata']); 
 
outputs:Array([0] =&gt; Array([id] =&gt;  ; 1 [用户名] =&gt; admin [firstname] =&gt; carl [lastname] =&gt; wirkus [密码] =&gt;密码[admin] =&gt; 1 [staff] =&gt; 0 [租户] =&gt;  0 [email] =&gt; test@test.com)
   
 
 

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

  echo“您当前登录时为:”。$ _SESSION ['userdata'] ['username']。“&lt; br&gt;”; 
 
outputs:您当前登录为:
 <  / code>  
 
 

我做错了什么?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dongtao9158 2017-04-18 14:19
    最佳回答

    Looks closer at your print_r($_SESSION['userdata']);. It's an array inside of an array. So you're not going deep enough to reach your values.

    echo "You are currently logged in as: " . $_SESSION['userdata'][0]['username'] . "<br>";
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题