<

Submitting a multidimensional array from a form to PHP via POST

Published on
30,487 Points
24,487 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
Comment
Author:mltsy
1 Comment
 

Expert Comment

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

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Join & Write a Comment

Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month