在PHP中爆炸会话变量总是显示“数组”

问题描述:

I try to explode the session variable named "Name" to get only the first name of the user.

However, when I try to echo it, the result is always "Array".

$_SESSION['name'] = $row['Name'];
echo explode(" ", $_SESSION['name']);

Result is: Array

I even tried to store the $_SESSION['Name'] in a variable and explode that variable instead and got the same result.

Any idea? Thanks!

我尝试爆炸名为“Name” code>的会话变量,只获取名字 但是,当我尝试 echo code>时,结果总是“Array” code>。 p>

  $ _ SESSION ['name'] = $ row ['Name']; 
echo explode(“”,$ _SESSION ['name']); 
  code>   pre> 
 
 

结果是: Array code>
p>

我甚至试图存储 $ _ SESSION ['Name'] code>在一个变量中,然后爆炸该变量并获得相同的结果。 p>

任何想法? 谢谢! p> div>

You want

$tempVar = explode(" ", $_SESSION['name']);
echo $tempVar[0];

explode() returns an array. You need to access it by index or assign them to individual variables:

$names = explode(" ", $_SESSION['name']);
echo $names[0];

Or:

list($first, $last) = explode(" ", $_SESSION['name']);
echo $first;