Grab data from array and combine to send email using PHP

别等时光非礼了梦想. 提交于 2019-12-12 03:53:56

问题


My code is:

$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();

Right now the mail only outputs a single entry such as:

Name is: Bob, age is: 20.

But I would like the email to output all the entries of the array when $b->getValue1() !== $b->getValue2() like:

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

How do I set up my content variable so that it grabs everything from the array and outputs it nicely in the email?

Thanks!


回答1:


Just append to $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;



回答2:


Just use .= instead of =

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


来源:https://stackoverflow.com/questions/35650419/grab-data-from-array-and-combine-to-send-email-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!