PDA

View Full Version : using foreach() with arrays.


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

Using:


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.

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

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

davidj
01-19-2008, 11:32 AM
where you have



$count=1;

foreach ($box as $cat) {

$count++;

$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

example...

$string = serialize($array);

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

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

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

nice one

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

http://www.webcheatsheet.com/PHP/multidimensional_arrays.php