56
<input name="foo[]" ... >

I've used these before, but I'm wondering what it is called and if there is a specification for it?

I couldn't find it in the HTML 4.01 Spec and results in various Google results only call it an "array" along with many PHP examples of processing the form data.

gak
  • 32,061
  • 28
  • 119
  • 154

4 Answers4

77

It's just PHP, not HTML.

It parses all HTML fields with [] into an array.

So you can have

<input type="checkbox" name="food[]" value="apple" />
<input type="checkbox" name="food[]" value="pear" />

and when submitted, PHP will make $_POST['food'] an array, and you can access its elements like so:

echo $_POST['food'][0]; // would output first checkbox selected

or to see all values selected:

foreach( $_POST['food'] as $value ) {
    print $value;
}

Anyhow, don't think there is a specific name for it

sqram
  • 7,069
  • 8
  • 48
  • 66
  • 3
    I think he understands this, he just wants to know if there's a spec for it. – Paolo Bergantino Jun 18 '09 at 05:56
  • As Paolo says, I do understand it! It is also used in other languages. – gak Jun 18 '09 at 06:01
  • 1
    PHP is the only language that I've encountered where the default form processing library requires special naming conventions for situations where you have more than one successful control with the same name. Other languages generally leave the determination to treat something as an array or not to the program author. – Quentin Jun 18 '09 at 08:49
  • 7
    The "spec", such as it is, is at http://us2.php.net/manual/en/faq.html.php#faq.html.arrays – Quentin Jun 18 '09 at 08:51
  • 1
    Further to David's comments: most server-side frameworks are smart enough to realise that if multiple form values have the same name (e.g. if three checkboxes with the same name were checked) then those values should be converted to an array when parsed on the server. For some reason, such an obvious approach has never been implemented in PHP: it requires the [] to tell it to do the sensible thing. Even Classic ASP knew better than that :-( – NickFitz Jun 18 '09 at 10:36
  • the order of the value array will be same as displaying the element? – Thamilan S Dec 12 '15 at 10:14
  • 1
    I'm coming here from Laravel's docs, they've named it "array inputs". (Link: https://laravel.com/docs/8.x/requests#retrieving-input) – aderchox Oct 07 '20 at 09:37
23

As far as I know, there isn't anything on the HTML specs because browsers aren't supposed to do anything different for these fields. They just send them as they normally do and PHP is the one that does the parsing into an array, as do other languages.

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
15

There are some references and pointers in the comments on this page at PHP.net:

Torsten says

"Section C.8 of the XHTML spec's compatability guidelines apply to the use of the name attribute as a fragment identifier. If you check the DTD you'll find that the 'name' attribute is still defined as CDATA for form elements."

Jetboy says

"according to this: http://www.w3.org/TR/xhtml1/#C_8 the type of the name attribute has been changed in XHTML 1.0, meaning that square brackets in XHTML's name attribute are not valid.

Regardless, at the time of writing, the W3C's validator doesn't pick this up on a XHTML document."

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
2

Follow it...

<form action="index.php" method="POST">
<input type="number" name="array[]" value="1">
<input type="number" name="array[]" value="2">
<input type="number" name="array[]" value="3"> <!--taking array input by input name array[]-->
<input type="number" name="array[]" value="4">
<input type="submit" name="submit">
</form>
<?php
$a=$_POST['array'];
echo "Input :" .$a[3];  // Displaying Selected array Value
foreach ($a as $v) {
    print_r($v); //print all array element.
}
?>
Rabby shah
  • 103
  • 1
  • 6