Home >Backend Development >PHP Tutorial >基本语法 如何往SESSION数组里添加元素

基本语法 如何往SESSION数组里添加元素

WBOY
WBOYOriginal
2016-06-23 13:33:211151browse

问一个很弱的基本语法。
如果是普通数组,我们可以这样:

$arr=array();$arr[]="1";$arr[]="2";print_r($arr);//这样就可以实现为$arr数组添加元素的目的


问题是,如果我定义的是一个SESSION数组:
$_SESSION['a']=array();//我要往$_SESSION['a']数组里添加1,2两个元素,应该怎么表示?


回复讨论(解决方案)

$a=array("a","b");$_SESSION["a"]=$a;  print_r($_SESSION["a"]);

有什么区别吗?

$_SESSION['a'][] = 1;$_SESSION['a'][] = 2;

$_SESSION['a']=array();$_SESSION['a'][] = 1;$_SESSION['a'][] = 2;

$_SESSION['a'][]=1;$_SESSION['a'][]=2;

你这就是二维数组了 在后面继续追加中括号就行

$_SESSION['a'][]="xxx"

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn