在PHP中将数组添加到会话数组中

时间:2016-09-16 11:47:24

标签: php

我正在尝试将数组(来自url的数据)添加到会话数组中:

$nbA = array(); $nbE = array(); $nbS = array();  $nbAll = array();
for ($a = 1; $a <= $_GET['nbAdultes']; $a++) { $nbA[] .= "ADULTE"; }
for ($e = 1; $e <= $_GET['nbEnfants']; $e++) { $nbE[] .= "ENFANT"; }
for ($s = 1; $s <= $_GET['nbSeniors']; $s++) { $nbS[] .= "SENIOR"; }
$nbAll = array_merge($nbA, $nbE, $nbS);
$nbAllSession = array_merge($nbAll, $nbAll);
$_SESSION['nbAllSession'] = $nbAllSession;
print_r($nbAllSession);
print_r($nbAll);

会话数组只显示2个数组

它应该是这样的:

1. Array ( [0] => ADULTE [1] => ENFANT [2] => ENFANT ) 

2. Array ( [0] => ADULTE [1] => ADULTE [2] => ENFANT )

最终会话数组:

Array ( [0] => ADULTE [1] => ENFANT [2] => ENFANT [3] => ADULTE [4] => ADULTE [5] => ENFANT) 

1 个答案:

答案 0 :(得分:0)

$cnt = 1;

$_GET['nbAdultes'] = $cnt;
$_GET['nbEnfants'] = $cnt;
$_GET['nbSeniors'] = $cnt;

$nbA = array(); 
$nbE = array(); 
$nbS = array();  
$nbAll = array();


for ($a = 1; $a <= $_GET['nbAdultes']; $a++) { $nbA[] = "ADULTE"; }
for ($e = 1; $e <= $_GET['nbEnfants']; $e++) { $nbE[] = "ENFANT"; }
for ($s = 1; $s <= $_GET['nbSeniors']; $s++) { $nbS[] = "SENIOR"; }


$nbAll = array_merge($nbA, $nbE, $nbS);


$nbAllSession = array_merge($nbAll, $nbAll);


$_SESSION['nbAllSession'] = $nbAllSession;

echo "<pre>";
print_r($nbAllSession);
print_r($nbAll);


//$nbA[] .= "ADULTE"; is not ok because $nbA[] is not **STRING**.