Posted by & filed under PHP.

This article is regarding an under-documented PHP feature when dealing with form data received from an HTML page (this works with both GET and POST data). If you name your HTML elements the proper way, you are able to send multidimensional arrays (or single dimension arrays for that matter) to your PHP script and access them using the $_POST superglobal array. You can use both associative arrays and numerical arrays. Pretty much any type of array that you can build in native PHP can be represented in an HTML form.

Here’s the syntax you’ve been waiting for. The first example will use numerical indexes, which will be automatically calculated by PHP, similar to doing $arr[] = ‘a’; $arr[] = ‘b’; :

<input name="address[]" value="1313 Mockingbird Ln" /> 
<input name="address[]" value="Appartment D" /> 
<input name="airport[4]" value="DET" /> 
<input name="airport[7]" value="JFK" />
<input name="person[john]" value="man" /> 
<input name="person[sue]" value="woman" />
<input name="person[john][name]" value="Johnathan" /> 
<input name="person[john][age]" value="39" /> 

Now, if we POST this data to the server, and do a <pre><?php print_r($_POST); ?></pre>, we will get the following data structure:

Array
(
    [address] => Array
        (
            [0] => 1313 Mockingbird Ln
            [1] => Appartment D
        )

    [airport] => Array
        (
            [4] => DET
            [7] => JFK
        )

    [person] => Array
        (
            [john] => Array
                (
                    [name] => Johnathan
                    [age] => 39
                )

            [sue] => woman
        )
)

And that’s all there is to it! My examples don’t really show off the usefulness of this feature though. The best usage is for iterating over a large list of data returned to the server, especially when dealing with dynamically generated lists of data inputs. For example, if you have an application where you need to edit X number of employees, and you make changes to only some of them (but not all of them), this will give you a list of specific items.

Thomas Hunter II

Thomas is the author of Advanced Microservices and a prolific public speaker with a passion for reducing complex problems into simple language and diagrams. His career includes working at Fortune 50's in the Midwest, co-founding a successful startup, and everything in between.
(Visited 5,624 times, 125 visits today)

Tags:

  • Ahsan

    i have similar problem i am using jsp and java servlet *chk check box
    chk resource4 yes Company Punjab Faisalabad 77
    chk resource16 eh Company Punjab Faisalabad 69
    chk resource16 yes Company Punjab Faisalabad 169
    chk resource17 yes Company Punjab Faisalabad 12

    i want to select a check box and then post the data of the row to Servlet
    the name of columns of the row are as follows checked[id], checked[id][name],checked[id][quantity]

    what i want is that when i select a row the entire data ion different columns get posted.
    i would highly appreciate your help
    thanks in advance

    • I have no idea… I’m a PHP developer, not a JSP developer.

  • ww

    What happens to $_POST, if you put all inputs named person[john] before person[sue] ?

    My result is that if 1-dimensional input person[key2] is *after* 2-dimensional input(s) person[key1][something] – then input “person” is sent as 1-dimensional array (apparently overriden by 1-dimensional input) and your [name] and [age] arrays are lost.