24 Votes

Send Form Input as an Array to PHP Script

Tip by Stefan Trost | Last update on 2023-12-19 | Created on 2012-10-29

It is often quite handy to receive inputs from a form directly as an array in a PHP script to be able to directly start processing the data in the correct form. In this tutorial, I will show you, how to do this.

Typical applications are, for example, forms with numerous fields of the same type or forms in which a previously unknown number of fields is created dynamically.

A first Example

First, let's look at a simple example. In our first example, we have three input fields and we want to receive their content in our PHP script "script.php" as just one array.

<form action="script.php" method="post">
  <input name="field[]" value="">
  <input name="field[]" value="">
  <input name="field[]" value="">
</form>

The key to being able to receive the data entered by the user directly as an array are the square brackets. The name "field" is arbitrary, but each input field must have the same name field[] so that all user entries are later in the same array.

In our PHP script we can receive the array with $_POST ['field'] and access the transferred values:

// output a single value
echo $_POST['feld'][0];

The first array field from our form has the index 0, the second 1 and so on. In our example we use $_POST ['field'][0] to access the value that the user entered in the first field.

We can work with the array like a normal PHP array:

// receive all data in an array
$fields = $_POST['field'];
  
// output or process all data
foreach ($fields as $value) {
  echo $value;
}

In this example we assign $_POST ['field'] to a variable, and then iterate through the array element by element. So we save ourselves the work of addressing and reading out each field individually.

Names of the Fields in the Array

In our first example, by writing [] we got a numerically indexed array. Alternatively, we can give the individual fields in the array names and thus get an associative array:

<form action="script.php" method="post">
  <input name="feld[firstname]" value="">
  <input name="feld[familyname]" value="">
  <input name="feld[password]" value="">
</form>

In our PHP script we can address the individual fields directly using their assigned names:

// receive all data in an array
$fields = $_POST['field'];
 
// read out fields by names
$firstname  = $fields['firstname'];
$familyname = $fields['familyname'];
$password   = $fields['password'];

This makes it easier to keep the individual fields apart.

More Possibilities

Of course you can also transfer any number of arrays next to each other and combine them with non-array fields:

<form action="script.php" method="post">
  <input name="field[]" value="">
  <input name="field[]" value="">
  <input name="otherarray[]" value="">
  <input name="normalfield" value="">
  <input name="otherarray[]" value="">
</form>

And then read out in PHP:

$field = $_POST['field'];
$otherarray = $_POST['otherarray'];
$normalfield = $_POST['normalfield'];
 
foreach ($field as $value) echo $value;
foreach ($otherarray as $value) echo $value;
echo $normalfield;

With this, you can define already in the form, in which form the data should be available later. In addition, as you can see, we are completely free in the arrangement and can mix normal fields between array fields. The order doesn't matter.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.