【问题标题】:Grab data from array and combine to send email using PHP从数组中获取数据并结合使用 PHP 发送电子邮件
【发布时间】:2016-06-09 14:12:25
【问题描述】:

我的代码是:

$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-&gt;getValue1() !== $b-&gt;getValue2() 喜欢时输出数组的所有条目:

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

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

谢谢!

【问题讨论】:

  • 追加 .您的 。内容 。到 。你的变量。和 。别 。覆盖。它。

标签: php arrays loops email associative-array


【解决方案1】:

只需使用 .= 而不是 =

这里 $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);
    }
}

【讨论】:

  • 谢谢,如果可以接受两个正确的答案,我会的。
【解决方案2】:

只需附加到 $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;

【讨论】:

  • 非常感谢,我遇到了一些错误,但现在都已修复 :)
猜你喜欢
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-17
  • 2012-06-15
  • 1970-01-01
  • 2012-10-25
相关资源
最近更新 更多