<

Submitting a multidimensional array from a form to PHP via POST

Published on
33,767 Points
27,767 Views
Last Modified:
Approved

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:
SIZES:
<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

COLORS:
<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

TYPES
<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

...
</form>

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).:

Array
(
    ...
    [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!

0
Author:mltsy
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free