从数组中获取数据并结合使用 PHP 发送电子邮件

Grab data from array and combine to send email using PHP

我的代码是:

$itemArray = array();
    foreach ($a->getDetails() as $b) {
        if ($b->getValue1() !== $b->getValue2()) {
            if (!array_key_exists($b->getId(), $itemArray)) {
                $itemArray[$b->getId()] = array('name' => $b->getName(), 'age' => $b->getAge());
        }
    }
}
if (count($itemArray) > 0 && $b->getValue1() !== $b->getValue2()) {
    foreach($itemArray as $item) {
        $personName = $item['name'];
        $personAge  = $item['age'] ;
        $content    = ('Name is: ' . $personName . ', age is: ' . $personAge);
    }
}

    $emailAddress = 'emailaddress@gmail.com';
    $fromEmail    = 'noreply@gmail.com';

    $body = <<<EOF
Here is an example email:

$content
EOF;

    $mail = new sfMail();
    $mail->setBody($body);
    $mail->send();

现在邮件只输出一个条目,例如:

Name is: Bob, age is: 20.

但我希望电子邮件在 $b->getValue1() !== $b->getValue2() 时输出数组的所有条目,例如:

Name is: Bob, age is 20:
Name is: John, age is 30.

如何设置我的内容变量,以便它从数组中获取所有内容并在电子邮件中很好地输出?

谢谢!

只需使用 .= 而不是 =

这里 $content .= ('Name is: ' . $personName . ', age is: ' . $personAge);

$content = "";
if (count($itemArray) > 0 && $b->getValue1() !== $b->getValue2()) {
    foreach($itemArray as $item) {
        $personName = $item['name'];
        $personAge  = $item['age'] ;
        $content    .= ('Name is: ' . $personName . ', age is: ' . $personAge);
    }
}

只需附加到 $content :)

$content = '';
if (count($itemArray) > 0 && $b->getValue1() !== $b->getValue2()) {
    foreach($itemArray as $item) {
        $personName  = $item['name'];
        $personAge   = $item['age'] ;
        $content    .= ('Name is: ' . $personName . ', age is: ' . $personAge) . "\n";
    }
}
// ... mail setting ...
$body = $content;