Display index name instead of 'array' when using nested foreach loops

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.

my form looks like this:

Beer: Wine: Liquor: There are many of these, it's a full grocery list.

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

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’

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?

You correct code will be similar to this:
[php]

$label = array(‘Type/Brand:’, ‘Quantity:’, ‘Notes:’);

foreach($_POST as $key1=>$arr1){
if (is_array($arr1)){
$message.=“Category: $key1\n”;
foreach($arr1 as $key2=>$arr2){
$message.=“Item: $key2\n”;
foreach($arr2 as $col => $val){
$val = stripslashes($val);
if($val!=’’) $message.=$label[$col].": $val\n";
}
}
}
}[/php]

You got it right on. I’ve been trying to grasp my head around the key and value pulling, and just couldn’t quite bend it there. Thank ya so much.

Ok, now I’m having a difficult time keeping the ‘item’ from being displayed if there is no input.
It’s doesn’t seem to be as easy placing an if($val!!=’’) after the second foreach loop, since $val hasn’t been created yet at that place.
What is the call and syntax for checking to see if the input is empty before writing $message.=“Item: $key2\n”; ?
I’m trying to keep the email pretty clean. If someone only orders 5 items, I’d like to not display the other couple of hundred items in the email as well.

Would you do something like:
if($_POST[][][]=> !=’’) ???

You can check if each of arrays is not zero length, before displaying header (Category, Item):

[php]
foreach($_POST as $key1=>$arr1){
if (is_array($arr1)){
if(count($arr1)>0) $message.=“Category: $key1\n”;
foreach($arr1 as $key2=>$arr2){
if(count($arr2)>0) $message.=“Item: $key2\n”;
foreach($arr2 as $col => $val){
$val = stripslashes($val);
if($val!=’’) $message.=$label[$col].": $val\n";
}
}
}
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service