Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts

    Multidimensional array in PHP

    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:

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

  • #2
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    Something like this?
    PHP Code:
    <?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.

  • Users who have thanked bacterozoid for this post:

    samuurai (08-08-2008)

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    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:
    Code:
    <input type=hidden name=item[]>
    and along with that, I want to store:
    Code:
    <input type=text name=item[order]>
    The order is entered by the user manually.

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Posts
    104
    Thanks
    7
    Thanked 2 Times in 2 Posts
    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:
    Code:
    <input type="text" name"item.order" />
    it would mean that...
    PHP Code:
    $_POST['item']['order'] = "Hello World"
    Try it and let me know if it works

    p.s.: your link don't work
    Last edited by NeoPuma; 08-08-2008 at 05:58 PM.

  • Users who have thanked NeoPuma for this post:

    samuurai (08-11-2008)

  • #5
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by NeoPuma View Post
    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:

    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?

  • #6
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Bump!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •