问题
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