2016-09-16 35 views
1

我想添加陣列(數據來自URL)到一個會話陣列:加入數組到session數組在PHP

$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) 
+0

'$ _SESSION [ 'nbAllSession'] [] = $ nbAllSession;'變化這樣和checck –

+0

同樣的事情...... – Clementine

+0

爲什麼這樣? '$ nbAllSession = array_merge($ nbAll,$ nbAll);' – CatalinB

回答

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**. 
+0

你需要舉一個正確的代碼的例子。否則有人會來爲你做,並挖掘任何認可。 – DevDonkey

+0

我已經寫過,不要像「。」那樣連接字符串之類的數組。 ($ a = 1; $ a <= $ _GET ['nbAdultes']; $ a ++){$ nbA [] =「ADULTE」; }' – CatalinB

+0

問題出在這裏:'$ nbAllSession = array_merge($ nbAll,$ nbAll);' – Clementine