如何将表单输入数组转换为 PHP 数组

我有一个像下面这样的表单,它被发布到 联系人,用户可以动态添加更多的 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);

所以我的输出是这样的:

名字是 姓名1,电子邮件是 电邮1,谢谢 名字是 姓名2,电子邮件是 电邮2,谢谢 名字是 姓名3,电子邮件是 电邮3,谢谢

434406 次浏览

E.g. by naming the fields like

<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]" />

(which is also possible when adding elements via JavaScript)

The corresponding PHP script might look like

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);

They are already in arrays: $name is an array, as is $email

So all you need to do is add a bit of processing to attack both arrays:

$name = $_POST['name'];
$email = $_POST['account'];


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

To handle more inputs, just extend the pattern:

$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";
}

However, VolkerK's solution is the best to avoid miss couple between email and username. So you have to generate HTML code with PHP like this:

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

Change $total_data to suit your needs. To show it, just like this:

$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);

Assuming the data was sent using POST method.

You can use an array of fieldsets:

<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>

I added a hidden field to count the number of the fieldsets. The user can add or delete the fields and then save it.

I came across this problem as well. Given 3 inputs: field[], field2[], field3[]

You can access each of these fields dynamically. Since each field will be an array, the related fields will all share the same array key. For example, given input data:

  • Bob, bob@bob.com, male
  • Mark, mark@mark.com, male

Bob and his email and sex will share the same key. With this in mind, you can access the data in a for loop like this:

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

This scales as well. All you need to do is add your respective array vars whenever you need new fields to be added.

Using this method should work:

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

You could do something such as this:

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, e.g.
//$_POST['keys'] = "values" would be in the array as "keys"=>"values"
$return[$key] = $value;
}
//Return the created array
return $return;
}

The test with:

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

This for me produced:

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)

This is an easy one:

foreach($_POST['field'] as $num => $val) {
print ' ' . $num . ' -> ' . $val . ' ';
}

Already is an array. My inputs are:

<input name="name[]" value='joe'>
<input name="lastname[]" value='doe'>
<input name="name[]" value='jose'>
<input name="lastname[]" value='morrison'>

In the $_POST data, returns the following:

[name] => Array
(
[0] => 'joe'
[1] => 'jose'
)
[lastname] => Array
(
[0] =>  'doe'
[1] => 'morrison'
)

You can access to these data, in the following way:

$names = $_POST['name']
$lastnames = $_POST['lastname']
// accessing
echo $names[0]; // joe

This way It is very useful for creating pivot tables.