在php中连接字符串和数组

时间:2012-06-22 09:27:28

标签: php arrays string concatenation

我有一个数组,我想用字符串连接它。

这是数组:

Array ( 
    [0] => Orange 1JD = 1
    [1] => Orange 5JD = 0
    [2] => Orange 10JD = 0
    [3] => Orange 20JD = 0
    [4] => Orange 50JD = 0
    [5] => Umniah 1JD = 0
    [6] => Umniah 5JD = 0
    [7] => Umniah 10JD = 0
    [8] => Umniah 20JD = 0
    [9] => Umniah 50JD = 0
    [10] => Zain 5JD = 0
    [11] => Zain 10JD = 0
    [12] => Zain 20JD = 0
    [13] => Zain 50JD = 0
) 

这是我的代码:

function emailSending($arrs){
    $to = getAdminEmail();
    $subject = "Card Quantity Alert!";
    $body = "Your Card is out of Quantity, Purchased more Card!\n\nThe Following Cards are:\n";
    foreach($arrs as &$arr){
        echo $arr;
    }
    echo $body;
    if (mail($to, $subject, $body)) {
        echo("<p>Message successfully sent!</p>");
    } else {
        echo("<p>Message delivery failed...</p>");
    }
}

$ body的值是否可能是这样的?

Your Card is out of Quantity, Purchased more Card! The Following Cards are:
Orange 1JD = 1
Orange 5JD = 0
Orange 10JD = 0
Orange 20JD = 0
Orange 50JD = 0
Umniah 1JD = 0
Umniah 5JD = 0
Umniah 10JD = 0
Umniah 20JD = 0
Umniah 50JD = 0
Zain 5JD = 0
Zain 10JD = 0
Zain 20JD = 0
Zain 50JD = 0

2 个答案:

答案 0 :(得分:4)

您可以使用join / implode将数组转换为字符串。

join("\r\n", $arrs)

答案 1 :(得分:1)

如果Array存储在$ array中,请尝试以下代码:

<?php
$body = 'Your Card is out of Quantity, Purchased more Card! The Following Cards are:' . "\n";

foreach ($array as $item) {
    $body .= $item . "\n";
}

print $body;
?>