2017-05-24 105 views
0

創建的關鍵字數組我試圖通過使用HTML輸入 創建的關鍵字數組,這裏是我的html輸入PHP通過HTML輸入

<form method="post" action=""> 
<input type="text" name="name[][you]" value="" /> 
<input type="text" name="name[][he]" value="" /> 
<input type="text" name="name[][she]" value="" /> 
<input type="text" name="name[][you]" value="" /> 
<input type="text" name="name[][he]" value="" /> 
<input type="text" name="name[][she]" value="" /> 
<button type="submit">go</button> 
</form> 

我outbut是

Array ([0] => Array ([you] => jhon) [1] => Array ([he] => joy) [2] => Array ([she] => sarah) [3] => Array ([you] => samm) [4] => Array ([he] => petter) [5] => Array ([she] => susan)) 

,但我想數組像這樣

Array([0]=> array ([you] => jhon [he] => joy [she] => sarah)[1]=> array ([you] => pitter [he] => tom [she] => suszan)) 

是有沒有去做到這一點

+0

爲什麼不寫確切的數字在括號 - ''等等? –

回答

1

嘗試這樣的==>

<form method="post" action=""> 
<input type="text" name="name[0][you]" value="" /> 
<input type="text" name="name[0][he]" value="" /> 
<input type="text" name="name[0][she]" value="" /> 
<input type="text" name="name[1][you]" value="" /> 
<input type="text" name="name[1][he]" value="" /> 
<input type="text" name="name[1][she]" value="" /> 
<button type="submit">go</button> 
</form> 

OR

<form method="post" action=""> 
    <?php $n = 2; // how many interval you want 
    for ($i = 0; $i < $n; $i++) { 
     ?> 
     <input type="text" name="name[<?php echo $i; ?>][you]" value="" /> 
     <input type="text" name="name[<?php echo $i; ?>][he]" value="" /> 
     <input type="text" name="name[<?php echo $i; ?>][she]" value="" /> 

<?php } ?> 
</form> 
+0

是就是這樣,但沒有在輸入 –

+0

@MounerMostafa的數字鍵後,嘗試通過我給 – Narayan

0

如果你想輸出有兩個孩子一個數組,然後手動設置鍵,名稱[0]爲前三個輸入和名稱[1]。

+0

所以沒有辦法做到這一點,而不在使用輸入密鑰號2號動態的解決方案? –

0

每次你寫name="[][key]" PHP自動增量的關鍵。 如果你寫的語法像[] PHP增加數組的索引。

小explation 例如:
如果寫這樣

$array[] = "msg1"; 
$array[] = "msg2"; 
$array[] = "msg3"; 

$array長度的陣列將是2(3個元素,因爲它從0開始),它是相同

$array[0] = "msg1"; 
$array[1] = "msg2"; 
$array[2] = "msg3"; 

這是different以上

$array[0] = "msg1"; 
$array[1] = "msg2"; 
$array[1] = "msg3"; 

此陣列將具有僅1個長度(僅2個元件)你的問題的

解決辦法是:

<form method="post" action=""> 
    <input type="text" name="name[0][you]" value="" /> 
    <input type="text" name="name[0][he]" value="" /> 
    <input type="text" name="name[0][she]" value="" /> 
    <input type="text" name="name[1][you]" value="" /> 
    <input type="text" name="name[1][he]" value="" /> 
    <input type="text" name="name[1][she]" value="" /> 
    <button type="submit">go</button> 
</form>