How to get form input array into PHP array

I have a form like the one below that is sent to contacts.php and the user can dynamically add more using 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[]" /> 

If output them in php with code below

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

I will get something like this:

name1name2name3email1email2email3

how can i get these arrays into something like the code below

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

so my conclusion is as follows:

Name name1 and email address email1 , thanks Name name2 and email address email2 , thank you
Name name3 and email3 , thanks

Thank you for any help or advice.

+67
arrays php forms
Jul 23 '10 at 0:35
source share
8 answers

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

So, all you have 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 stretch 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"; } 
+116
Jul 23 2018-10-10T00: 00Z
source share
— -

eg. by naming fields such as

 <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 through javascript)

Relevant 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); 
+35
Jul 23 2018-10-10T00:
source share

What if you have an array of fields?

 <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 fields. The user can add or remove fields, and then save them.

+2
Jul 25 2018-10-25T00:
source share

I ran into this problem. Given the three inputs: field [], field2 [], field3 []

You can access each of these fields dynamically. Since each field will be an array, the associated fields will have a common array key. For example, given the input:

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

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

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

It also scales. All you have to do is add your respective arrays when you need new fields.

+1
Jan 07 '13 at
source share

I know him a little later, but you can do something like 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, eg //$_POST['keys'] = "values" would be in the array as "keys"=>"values" $return[$key] = $value; } //Return the created array return $return; } 

Test with:

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

This is for me created:

 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) 
+1
Jul 10 '15 at 14:30
source share

However, VolkerK is the best solution to avoid gaps between email and username. So you should generate HTML 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 this, simply 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 the POST method.

0
Jul 23 '10 at 6:30
source share

However, you can use the code below as,

 $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>'; } 
0
Apr 13 '17 at 10:33
source share

Using this method should work:

 $name = $_POST['name']; $email = $_POST['account']; while($explore=each($email)) { echo $explore['key']; echo "-"; echo $explore['value']; echo "<br/>"; } 
-one
Apr 23 '15 at 21:50
source share



All Articles