View Full Version : Multidimensional array in PHP

08-08-2008, 04:58 PM

I'm trying to figure out how to use multidimensional arrays in php forms.

what is the correct code?

I've tried testing with lots of different ways, but can't figure it out:


<input type=text name="testarray[thing][]">

08-08-2008, 05:12 PM
Something like this?


$theArray = array( array("element1","element2"), array("element3","element4") );

echo '<form><input type="text" value="' . $theArray[1][0] . '"></form>';


The text box will display "element3" inside of it. You can set any property like this.

08-08-2008, 05:21 PM
Thanks, maybe I didn't explain my problem properly...

I want to store a multi-dimensional array inside a $_POST variable.

What's the syntax for that... if it's possible.

The reason is..

I want to have two input tags.. one containing the names of each item, and the other which stores the order of each item. I then want to run asort so sort them and display them in the correct order.


<input type=hidden name=item[]>

and along with that, I want to store:

<input type=text name=item[order]>

The order is entered by the user manually.

08-08-2008, 05:49 PM
Don't quote me on this, but I think if you name an input field with a ., then its sent as an array.

For example, if I put "Hello World" into the following text box:

<input type="text" name"item.order" />
it would mean that...

$_POST['item']['order'] = "Hello World";

Try it and let me know if it works :)

p.s.: your link don't work

08-11-2008, 07:59 AM
Don't quote me on this, but I think if you name an input field with a ., then its sent as an array.

No such luck on that one.

Ok, i've made some headway..

Here's my code:


$thingy = array("number1", "number2", "number3");


foreach($_GET['thingy'] as $thing)
echo $thing . "'s order is <b>" . $_GET[$thing]['order'] . "</b><br>";

else { ?>
<form action="test.php" method="get">
foreach($thingy as $thing)
{ ?>
<input type="text" name="<?php echo $thing . "[order]";?>">
<input type="hidden" name="thingy[]" value="<?php echo $thing; ?>">

<input type="submit" name="submit">
} ?>

Now, how do I order the thingy[] array using the $thing[order] array?

08-11-2008, 05:35 PM