Using square brackets in hidden HTML input fields

It passes data as an array to PHP. When you have HTML forms with the same name it will append into comma lists like checkbox lists. Here PHP has processing to convert that to a PHP array based on the [] like so:

To get your result sent as an array to your PHP script you name the , or elements like this:

<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />

Notice the square brackets after the variable name, that’s what makes it an array. You can group the elements into different arrays by assigning the same name to different elements:

<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyOtherArray[]" />
<input name="MyOtherArray[]" />

This produces two arrays, MyArray and MyOtherArray, that gets sent to the PHP script. It’s also possible to assign specific keys to your arrays:

<input name="AnotherArray[]" />
<input name="AnotherArray[]" />
<input name="AnotherArray[email]" />
<input name="AnotherArray[phone]" />

http://us2.php.net/manual/en/faq.html.php

Leave a Comment