View Full Version : using foreach() with arrays.

01-19-2008, 10:10 AM
I have a process where users selects a number of check boxes and I capture these in an array.


foreach ($box as $cat) {
echo $cat."-";
Will return box1-box2-box4 etc

Can I capture box1-box2-box4 in a session?

If I use $_SESSION['boxes']=$cat;

I only get the last result. In the example above it would be $box4. Which I understand but I just can't seem to work out how to do it.

I can create a text field and apply the foreach example shown above as the value. Then capture the value on the next page as $_SESSION['boxes']=$_POST['textfield'];

As I'm posting this I'm thinking if I turn the array into a string that may work. Any suggestions would be appreciated.

01-19-2008, 10:17 AM
Here's the answer I think. Well it worked anyway.

$dash_separated = implode("-", $box);

01-19-2008, 10:32 AM
where you have


foreach ($box as $cat) {


$sessName = "box".$count;

$_SESSION[$sessName] = $cat;


that should generate a seperate session which i supose you could concat them into a string (a bit long winded)

look into serialize


$string = serialize($array);

01-29-2008, 10:47 AM
you could store them again in an array using davidj's example with

$_SESSION[$sessName][$count] = $cat;

01-30-2008, 12:26 PM
a ticket to multi-dimensional city!

nice one

01-31-2008, 09:06 AM
I think this is a very clear tut for understanding multi dimensional arrays: