0

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!

1
  • 2
    Append . your . content . to . your variable . and . don't . overwrite . it. Commented Feb 26, 2016 at 11:24

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, I ran into some errors but all fixed now :)
1

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

1 Comment

Thanks, if it was possible to accept two answers as correct I would.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.