我可以在php中的SESSION数组上使用array_push吗? [英] Can I use array_push on a SESSION array in php?

查看:172
本文介绍了我可以在php中的SESSION数组上使用array_push吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在多个页面上使用的数组,所以我将其设置为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆