我可以在 php 中的 SESSION 数组上使用 array_push 吗? [英] Can I use array_push on a SESSION array in php?
问题描述
我有一个需要在多个页面上显示的数组,因此我将其设为 SESSION 数组.我想添加一系列名称,然后在另一个页面上,我希望能够使用 foreach 循环来回显该数组中的所有名称.
I have an array that I want on multiple pages, so I made it a SESSION array. I want to add a series of names and then on another page, I want to be able to use a foreach loop to echo out all the names in that array.
这是会话:
$_SESSION['names']
我想像这样使用 array_push 向该数组添加一系列名称:
I want to add a series of names to that array using array_push like this:
array_push($_SESSION['names'],$name);
我收到此错误:
array_push() [function.array-push]:第一个参数应该是一个数组
array_push() [function.array-push]: First argument should be an array
我可以使用 array_push 将多个值放入该数组吗?或者也许有更好、更有效的方式来完成我正在努力实现的目标?
Can I use array_push to put multiple values into that array? Or perhaps there is a better, more efficient way of doing what I am trying to achieve?
推荐答案
是的,你可以.但是第一个参数应该是一个数组.
Yes, you can. But First argument should be an array.
所以,你必须这样做
$_SESSION['names'] = array();
array_push($_SESSION['names'],$name);
我个人从不使用 array_push,因为我认为这个函数没有意义.而我只是使用
Personally I never use array_push as I see no sense in this function. And I just use
$_SESSION['names'][] = $name;
这篇关于我可以在 php 中的 SESSION 数组上使用 array_push 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!