How to display name instead of 'Array' when displaying multidimensional array in form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drewschott

    How to display name instead of 'Array' when displaying multidimensional array in form

    I'm having a problem receiving the full amount of information i need on a grocery order from that i'm accessing inputted values using form to mail in php.

    a snippet of my form looks like this:
    Code:
    <html>
    <body>
    <form action="...php" method=POST...
    <tr>
                  <th scope="col">Beer:</th>
                  <th scope="col"><input type="text" name="BeerandWine[Beer][]" /></th>
                  <th scope="col"><input type="text" name="BeerandWine[Beer][]" /></th>
                  <th scope="col"><input type="text" name="BeerandWine[Beer][]" /></th>
                  </tr>
                <tr>
                  <th scope="col">Wine:</th>
                  <th scope="col"><input type="text" name="BeerandWine[Wine][]" /></th>
                  <th scope="col"><input type="text" name="BeerandWine[Wine][]" /></th> 
                  <th scope="col"><input type="text" name="BeerandWine[Wine][]" /></th>      
                  </tr>
                <tr>
                  <th scope="col">Liquor:</th>
                  <th scope="col"><input type="text" name="BeerandWine[Liquor][]" /></th>
                  <th scope="col"><input type="text" name="BeerandWine[Liquor][]" /></th>
                  <th scope="col"><input type="text" name="BeerandWine[Liquor][]" /></th>
                  </tr>
    ...</form>
    </body>
    </html>
    There are many of these, it's a full grocery list.

    here is my php code to send it to my email:

    Code:
    <?php
    foreach($_POST as $key){
             if (is_array($key)){
             $message.="Category: $key\n";
                foreach($key as $row){
                   $message.="Item: $row\n";
                foreach($row as $col => $val){
                $val = stripslashes($val);
                      $message.="$col: $val\n";
                }
             } 
           }
    }
    ?>
    And here is what shows up in my email:

    Category: Array
    Item: Array
    0: Stella
    1: 6 pack
    2: Cold
    Item: Array
    0: Merlot
    1: 750ml
    2:
    Item: Array
    0: Whiskey
    1:
    2: With cup


    I'ts really a very handy script, except i can't seem to gather anything but 'array' from $key and $row.
    How can I make the index name of the nested array display in my email, instead of 'array'. Keeping in mind I have a few hundred inputs.

    This is how i want it to look:

    Category: BeerandWine
    Item: Beer
    0: Stella
    1: 6 pack
    2: Cold
    Item: Wine
    0: Merlot
    1: 750ml
    2:
    Item: Liquor
    0: Whiskey
    1:
    2: With cup

    and i also want to have:
    Type/Brand:, Quantity:, Notes:
    instead of:
    0:, 1:, 2:
    and not display the empty values, but those I could probably find on my own after I solve this...I've been searching for a solution to this for days. Any help?
    Last edited by Niheel; Oct 25 '10, 06:42 AM. Reason: please use code tags to display code
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    To print out an array you need print_r($array) .
    I find formating is much tidier. Use
    Code:
    '<pre>Data '.print_r($array,1).'</pre>'
    Note the '1' as second parameter in print_r.
    You will still get the word 'array'. The only way I have found to remove this is with str_replace('Ar ray','',string)

    Comment

    Working...