I have this variable that contains multiple values and I want to save all the values into a $_SESSION['gamecode']
. It displays only the last value.
$var=explode("|",$key);
$gamecode=trim($var[0]);
session_start();
$gc[]= trim($var[0]);
$_SESSION['gamecode'][]=$gc;
var_dump($_SESSION['gamecode']);
EDITED
foreach($_POST['gm'] as $key => $answer){
if($answer != ''){
$var=explode("|",$key);
$gamecode=trim($var[0]);
$_SESSION['gamecode'][]=$gc;
var_dump($_SESSION['gamecode']);
EDIT 2
foreach($_POST['gm'] as $key => $answer){
if($answer != ''){
$var=explode("|",$key);
$gamecode=trim($var[0]);
}
$_SESSION['gamecode'][]=$gc;
var_dump($_SESSION['gamecode']);
i figured it out. by starting the Session in the foreach loop and calling it outside the loop. it works fine. Thanks
foreach($_POST['gm'] as $key => $answer){
if($answer != ''){
$var=explode("|",$key);
$gamecode=trim($var[0]);
$gc[]= trim($var[0]);
$_SESSION['gamecode']=$gc;
}
var_dump($_SESSION['gamecode']);
if you want all the values
$var=explode("|",$key);
$_SESSION['gamecode']=$var;
or just
$_SESSION['gamecode']=explode("|",$key);
currently your just storing the first one.
$var[0]
is the first array element after the explode
So are you trying to have $_SESSION['gamecode'] be an array of piped values? Or $_SESSION['gamecode'] to be an array in and of itself?
It's not completely clear what you're trying to do here.
It looks like you've got piped values in $key, that you're making into array $var, and extracting the first value to $gamecode.
There's a lot of redundancy here, and you're not being explicit about what you want.
If you're trying to have $_SESSION['gamecode'] be an array with multiple values, you'd want to do something like
session_start();
$var=explode("|",$key);
$_SESSION['gamecode'] = $var;
print_r($_SESSION['gamecode']);
Else if you want it to be a string of multiple values, it might be more fruitful to make $key a JSON string, no?
Not really enough details for me to give you a succinct answer.