如何将表单input数组转换为PHP数组

我有一个像下面这个发布到contacts.php的表单,用户可以dynamic地添加更多的jQuery。

<input type="text" name="name[]" /> <input type="text" name="email[]" /> <input type="text" name="name[]" /> <input type="text" name="email[]" /> <input type="text" name="name[]" /> <input type="text" name="email[]" /> 

如果我使用下面的代码在PHP中回显出来

 $name = $_POST['name']; $email = $_POST['account']; foreach( $name as $v ) { print $v; } foreach( $email as $v ) { print $v; } 

我会得到这样的东西:

name1name2name3email1email2email3

我怎么能得到这些数组像下面的代码

 function show_Names($n, $m) { return("The name is $n and email is $m, thank you"); } $a = array("name1", "name2", "name3"); $b = array("email1", "email2", "email3"); $c = array_map("show_Names", $a, $b); print_r($c); 

所以我的输出是这样的:

姓名是name1 ,email是email1 ,谢谢
名字是name2 ,email是email2 ,谢谢
名字是name3 ,email是email3 ,谢谢

感谢您的任何帮助或build议

他们已经在数组中: $name是一个数组, $email

所以你所要做的就是添加一些处理来攻击这两个数组:

 $name = $_POST['name']; $email = $_POST['account']; foreach( $name as $key => $n ) { print "The name is ".$n." and email is ".$email[$key].", thank you\n"; } 

要处理更多的投入,只需扩展模式:

 $name = $_POST['name']; $email = $_POST['account']; $location = $_POST['location']; foreach( $name as $key => $n ) { print "The name is ".$n.", email is ".$email[$key]. ", and location is ".$location[$key].". Thank you\n"; } 

例如通过命名字段

 <input type="text" name="item[0][name]" /> <input type="text" name="item[0][email]" /> <input type="text" name="item[1][name]" /> <input type="text" name="item[1][email]" /> <input type="text" name="item[2][name]" /> <input type="text" name="item[2][email]" /> 

(这通过javascript添加元素也是可能的)

相应的php脚本可能看起来像

 function show_Names($e) { return "The name is $e[name] and email is $e[email], thank you"; } $c = array_map("show_Names", $_POST['item']); print_r($c); 

如果你有一系列的字段集呢?

 <fieldset> <input type="text" name="item[1]" /> <input type="text" name="item[2]" /> <input type="hidden" name="fset[]"/> </fieldset> <fieldset> <input type="text" name="item[3]" /> <input type="text" name="item[4]" /> <input type="hidden" name="fset[]"/> </fieldset> 

我添加了一个隐藏字段来计算字段集的数量。 用户可以添加或删除字段,然后保存。

我也遇到了这个问题。 给定3个input:字段[],字段2 [],字段3 []

您可以dynamic访问每个这些字段。 由于每个字段将是一个数组,相关的字段将全部共享相同的数组密钥。 例如,给定的input数据:

  • 鲍勃,bob@bob.com,男
  • 马克,mark@mark.com,男

鲍勃和他的电子邮件和性别将共享相同的密钥。 考虑到这一点,您可以像这样访问for循环中的数据:

  for($x = 0; $x < count($first_name); $x++ ) { echo $first_name[$x]; echo $email[$x]; echo $sex[$x]; echo "<br/>"; } 

这也是一样的。 所有你需要做的是添加你的相应的数组variables,每当你需要添加新的领域。

我知道现在有点晚了,但是你可以做这样的事情:

 function AddToArray ($post_information) { //Create the return array $return = array(); //Iterate through the array passed foreach ($post_information as $key => $value) { //Append the key and value to the array, eg //$_POST['keys'] = "values" would be in the array as "keys"=>"values" $return[$key] = $value; } //Return the created array return $return; } 

testing:

 if (isset($_POST['submit'])) { var_dump(AddToArray($_POST)); } 

这为我生产:

 array (size=1) 0 => array (size=5) 'stake' => string '0' (length=1) 'odds' => string '' (length=0) 'ew' => string 'false' (length=5) 'ew_deduction' => string '' (length=0) 'submit' => string 'Open' (length=4) 

但是,VolkerK的解决scheme是最好的避免电子邮件和用户名之间的怀念夫妇。 所以你必须像这样用PHP生成HTML代码:

 <? foreach ($i = 0; $i < $total_data; $i++) : ?> <input type="text" name="name[<?= $i ?>]" /> <input type="text" name="email[<?= $i ?>]" /> <? endforeach; ?> 

更改$ total_data以满足您的需求。 为了显示它,就像这样:

 $output = array_map(create_function('$name, $email', 'return "The name is $name and email is $email, thank you.";'), $_POST['name'], $_POST['email']); echo implode('<br>', $output); 

假设数据是使用POST方法发送的。

尽pipe如此,你可以使用下面的代码,

 $a = array('name1','name2','name3'); $b = array('email1','email2','email3'); function f($a,$b){ return "The name is $a and email is $b, thank you"; } $c = array_map('f', $a, $b); //echoing the result foreach ($c as $val) { echo $val.'<br>'; } 

使用这种方法应该工作:

 $name = $_POST['name']; $email = $_POST['account']; while($explore=each($email)) { echo $explore['key']; echo "-"; echo $explore['value']; echo "<br/>"; }