...

View Full Version : Multidimensional array in PHP



samuurai
08-08-2008, 04:58 PM
Hi,

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:

eg:



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

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


<?PHP

$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.

samuurai
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.

eg:


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

NeoPuma
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

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



<?php

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


if(isset($_GET['submit']))

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

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

<input type="submit" name="submit">
<?php
} ?>
</form>


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

samuurai
08-11-2008, 05:35 PM
Bump!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum