2017-05-24 109 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>