Submitting a multidimensional array from a form to PHP via POST


After much searching on the subject myself, I've found that many people have wondered whether it's possible to submit a multidimensional array from an HTML form to a PHP script.  And for some reason, all the articles answering this query are convoluted or wrong.  The answer is simple: YES!

It is not only possible, but very straight-forward to submit a multi-dimensional array from a form to a PHP script.  The functionality was added in PHP 4 (and nobody but... well nobody... should still be using a version of PHP prior to 4).   Here is how it works:

Let's say you want to let someone pick several "options" to make available for a new product on your web page, so you have some check boxes for colors, sizes and types.  But you have lots of other data in your form you don't want to get it confused with (perhaps there's another "type" of something else also in your form), so you want to contain all of these selections in a sub array called "options" so you can find them easily once you get to the PHP.  Here's how that would look in XHTML:

<form action="process.php" method="post">
                      check which options this product will be available in:
                      <input type="checkbox" name="options[sizes][]" value="small" />small
                      <input type="checkbox" name="options[sizes][]" value="medium" />medium
                      <input type="checkbox" name="options[sizes][]" value="medium" />large
                      <input type="checkbox" name="options[colors][]" value="red" />red
                      <input type="checkbox" name="options[colors][]" value="blue" />blue
                      <input type="checkbox" name="options[colors][]" value="green" />green
                      <input type="checkbox" name="options[colors][]" value="orange" />orange
                      <input type="checkbox" name="options[types][]" value="floppy" />floppy
                      <input type="checkbox" name="options[types][]" value="compact" />compact
                      <input type="checkbox" name="options[types][]" value="hard" />hard
                      <input type="checkbox" name="options[types][]" value="digital" />digital
                      <input type="checkbox" name="options[types][]" value="analog" />analog

Open in new window

And as long as you're running a version of PHP that is not ancient, your $_POST array will look something like this (depending on options checked).:

                          [options] => Array
                                  [sizes] => Array
                                          [0] => small
                                          [1] => large
                                  [colors] => Array
                                          [0] => red
                                          [1] => green
                                  [types] => Array
                                          [0] => floppy
                                          [1] => analog
                                          [2] => digital

Open in new window

Pretty much how you'd expect it to look.  Nothin' tricky about it!  So don't worry, go ahead and use them; they work!


Comments (1)

Assuming I want to echo these results individually how would I go about it. I have tried different methods none works efficiently

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.