By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,226 Members | 1,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,226 IT Pros & Developers. It's quick & easy.

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

P: n/a
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:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <form action="...php" method=POST...
  4. <tr>
  5.               <th scope="col">Beer:</th>
  6.               <th scope="col"><input type="text" name="BeerandWine[Beer][]" /></th>
  7.               <th scope="col"><input type="text" name="BeerandWine[Beer][]" /></th>
  8.               <th scope="col"><input type="text" name="BeerandWine[Beer][]" /></th>
  9.               </tr>
  10.             <tr>
  11.               <th scope="col">Wine:</th>
  12.               <th scope="col"><input type="text" name="BeerandWine[Wine][]" /></th>
  13.               <th scope="col"><input type="text" name="BeerandWine[Wine][]" /></th> 
  14.               <th scope="col"><input type="text" name="BeerandWine[Wine][]" /></th>      
  15.               </tr>
  16.             <tr>
  17.               <th scope="col">Liquor:</th>
  18.               <th scope="col"><input type="text" name="BeerandWine[Liquor][]" /></th>
  19.               <th scope="col"><input type="text" name="BeerandWine[Liquor][]" /></th>
  20.               <th scope="col"><input type="text" name="BeerandWine[Liquor][]" /></th>
  21.               </tr>
  22. ...</form>
  23. </body>
  24. </html>
There are many of these, it's a full grocery list.

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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. foreach($_POST as $key){
  3.          if (is_array($key)){
  4.          $message.="Category: $key\n";
  5.             foreach($key as $row){
  6.                $message.="Item: $row\n";
  7.             foreach($row as $col => $val){
  8.             $val = stripslashes($val);
  9.                   $message.="$col: $val\n";
  10.             }
  11.          } 
  12.        }
  13. }
  14. ?>
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?
Oct 25 '10 #1
Share this Question
Share on Google+
1 Reply


code green
Expert 100+
P: 1,726
To print out an array you need print_r($array).
I find formating is much tidier. Use
Expand|Select|Wrap|Line Numbers
  1. '<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('Array','',string)
Oct 25 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.