循环foreach并设置为段落

时间:2013-04-28 07:12:42

标签: php arrays

我有这个数组:

    $array = array( array( Title => "rose", 
                  Price => 1.25,
                  Number => 15 
                ),
           array( Title => "daisy", 
                  Price => 0.75,
                  Number => 25,
                ),
           array( Title => "orchid", 
                  Price => 1.15,
                  Number => 7 
                )
         );

我使用foreach从数组中删除每个数据,         $ header =“我想将这种格式发送到我的电子邮箱:”;

    $closing = "Thank you.";
    foreach($array as $row){
    $title = row['Title'];
    $price = row['Price'];
    $number = row['Number'];


    $details ="
    Title: $title
    Price: $price 
    Number: $number
    ";
    $message ="
    $headers

    $details

    $closing
    ";
    }

    mail($email, $data, $detail);

    //And I want to mail the result with a format same as below,

我想将这种格式发送到我的电子邮箱:

这是第一条记录:

标题:数据

价格:数据

数字:数据 ,

标题:数据

价格:数据

数字:数据 ,

标题:数据

价格:数据

数字:数据

谢谢。

但它只显示我的数组中的1个数据

如何使用这种格式显示数组中的所有数据?需要帮助^ _ ^

3 个答案:

答案 0 :(得分:0)

尝试在循环外部移动消息分配并在循环中构建详细信息值

$closing = "Thank you.";
$details = '';
foreach($array as $row){
    $title = $row['Title'];
    $price = $row['Price'];
    $number = $row['Number'];


    $details .= "
Title: $title
Price: $price 
Number: $number
";
}
$message ="
$headers

$details

$closing
";

mail($email, 'subject', $message);

http://codepad.org/Qi9RZs2Y

答案 1 :(得分:0)

您需要append下一个提交值,例如

// loop {
 $details .="
Title: $title
Price: $price 
Number: $number
";

// }
$message ="
$headers

$details

$closing
";

答案 2 :(得分:0)

$details= "";
foreach($array as $row){
    $details.= "Title: {$row['Title']}
    Price: {$row['Price']} 
    Number: {$row['Number']}";
}

您的foreach不正确请尝试使用此代码