This is the output of <pre>print_r($_POST)</pre> from a form I am building.

Array
(
    [theArray] => Array
        (
            [1] => Array
                (
                    ['name'] => foo
                )

        [2] => Array
            (
                ['name'] => bar
            )

    )

[submit] => submit
)

What I am trying to do is to be able to directly address the string data inside the array, i.e.:

echo $_POST['theArray'][1]['name'];

This should print the string "foo."

Unfortunately, this does not work. I have done many, many tests that confirm that $_POST['theArray'][1] is indeed an array, but any attempts to access it (['name'], [0], [1], etc.) have returned errors.

My only successful attempt at accessing the data is the following:

foreach ($_POST["theArray"] as $key) {

if(is_array($key)){
foreach ($key as $key2 => $value) {

echo $value."<br />";

}
} 

}

I am wanting to add more parameters besides 'name' eventually, which is why I need to be able to access the data directly. Also, I ask that you not suggest I simply use a database. This script needs to be portable.

Thank you for any help you can offer.

    Notice any difference between these two lines?

    [theArray] =>
    ['name'] =>

    Try accessing your element with

    echo $_POST['theArray'][1]["'name'"];
    

      I am absolutely dumbfounded. I have put a solid eight hours into diagnosing and troubleshooting this problem when the solution was this simple.

      Thank you so much.

        Write a Reply...