views:

177

answers:

2

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.

This is the session:

$_SESSION['names']

I want to add a series of names to that array using array_push like this:

array_push($_SESSION['names'],$name);

I am getting this error:

array_push() [function.array-push]: First argument should be an array

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?

+3  A: 

Yes, you can. But First argument should be an array.

So, you must do it this way

$_SESSION['names'] = array();
array_push($_SESSION['names'],$name);

Personally I never use array_pus as I see no sense in this function. And I just use

$_SESSION['names'][] = $name;
Col. Shrapnel
Thanks for the great suggestion!
zeckdude
+5  A: 

Try with

if (!isset($_SESSION['names'])) {
    $_SESSION['names'] = array();
}
array_push($_SESSION['names'],$name);
hsz
Thank you for your help!
zeckdude