Sudoscience
Sudoscience

Reputation: 301

Grab data from array and combine to send email using PHP

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 = '[email protected]';
    $fromEmail    = '[email protected]';

    $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!

Upvotes: 0

Views: 69

Answers (2)

Halayem Anis
Halayem Anis

Reputation: 7785

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;

Upvotes: 2

Vivek Pipaliya
Vivek Pipaliya

Reputation: 498

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

Upvotes: 1

Related Questions