PHP将foreach循环中的多个值解析为其他php文件

I'm trying to get multiple values out a foreach loop from file one.php to file two.php. I'm trying to parse them with:

from file one.php:

var_dump($key) //this gives string(19) "key1" string(19) "key2" string(19) "key3"
       session_start(); 
       $_SESSION['thekeys'] = $key;

to two.php

session_start(); 
echo $_SESSION['thekeys'];

However the echo in file two.php gives only the last key ("key3"). How would I parse all the values to two.php? Should I make it in an array or some?

Thanks in advance!

You are overriding your keys in every foreach loop.

Just use an array to pass it. This could be helpful: http://www.phpriot.com/articles/intro-php-sessions/7

Why don't you use a foreach and then send the new array to the $_SESSION array:

one.php

$_SESSION['arrayOfKeys'][] = $tempArray;

two.php:

print_r($_SESSION['arrayOfKeys']);

It should work.

Assuming you have the following foreach loop:

foreach($keys as $key)
{


}  

Then try this snippet:

$keys_array
foreach($keys as $key)
{
   $keys_array[] = $key;
}
session_start(); 
$_SESSION['thekeys'] = $keys_array;
//var_dump($_SESSION['thekeys'] => key1, key2, key3