foreach into 1 variable

3 posts by 2 authors in: Forums > CMS Builder
Last Post: November 7, 2013   (RSS)

By zaba - November 6, 2013

Hi, I have a foreach loop of email addresses that I need to combine into 1 variable for use later in my script, each email needs a coma separating it (but not at the end)

    <?php foreach ($notifyRecords as $record): ?>
      <?php echo $record['notify_email'] ?> ,
    <?php endforeach ?>

So I want the resulting variable to be something like this built from the foreach statement above:

$recipients='bob@margaret.com, margaret@brian.com, maureen@bob.com, sid@margaret.com';

By Brownleather - November 6, 2013

Hi Zaba

This should do it

<?
$recipients = array();

foreach ($notifyRecords as $record) {
    $recipients[] = $record['notify_email'];
}

$recipients = implode(', ', $recipients);

echo $recipients;
?>

Enjoy