Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Session Variables

Posted on 2004-09-02
5
Medium Priority
?
243 Views
Last Modified: 2006-11-17
I know how to use session variables but I would like to know if it is possible to use an array for a session variable. If so is there anything special that one needs to do.

Thanks for the help

Richard G Hurley
0
Comment
Question by:RICHARDH
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 200 total points
ID: 11966481
various method, examples:

$_SESSION['anarray'] = array("one",two","three","etc");

or

$_SESSION['anarray'][] = "one";
$_SESSION['anarray'][] = "two";
$_SESSION['anarray'][] = "three";
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11966514
The above will use numeric indexes for each array items (as normal) beginning with 0 and incrementing for each item. If you want associative indexes you can do:

$_SESSION['anarray'] = array("key1" => "one", "key2" => "two", "key3" => "three", "key4" => "etc");

or

$_SESSION['anarray']['key1'] = "one";
$_SESSION['anarray']['key2'] = "two";
$_SESSION['anarray']['key3'] = "three";

so

echo $_SESSION['anarray']['key2'];

will output the string "two";
0
 

Author Comment

by:RICHARDH
ID: 11966696
Thanks for the help.

I assume that one the session array has been set up, I can keep referring to it and adding new values.

0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11966719
Thats correct, just think of it as a global array (which is effectively what it is).
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question