0

My array named $array is like:

Array
(
[0] => Array
    (
        [id] => 2459
        [email] => example1@yahoo.com
        [action] => waiting
        [udid] => 1232123212321232123212321232123234523452
    )

[1] => Array
    (
        [id] => 2462
        [email] => example2@yahoo.com
        [action] => waiting
        [udid] => b851125907768199c03ab24887fdfea96e3183a6
    )

[2] => Array
    (
        [id] => 2463
        [email] => example3@yahoo.com
        [action] => waiting
        [udid] => f7ddf0b4596ea5d6c23fad0ab0f8f92a61f2103d
    )

)

So what I'm looking for is a string like:

$emails= "example1@yahoo.com,example2@yahoo.com,example3@yahoo.com"

to use is mail()

mail($emails, "Confirmation Email", $body, $headers);

However I can use file_put_content & file_get_content to store and retrieve the values, I'm looking for a short, better and professional way!

Any ideas would be appreciated!

PersianHero
  • 87
  • 2
  • 10

2 Answers2

2

You can convert your array into that string using the array_column and implode functions.

<?php

$values = [
    ['id' => 1, 'email' => 'email1@gmail.com'],
    ['id' => 2, 'email' => 'email2@gmail.com'],
    ['id' => 3, 'email' => 'email3@gmail.com'],
    ['id' => 4, 'email' => 'email4@gmail.com'],
];

$emails = implode(',', array_column($values, 'email'));

echo $emails;
William Okano
  • 370
  • 1
  • 3
  • 10
0

How about something like this?

$emails = array();
foreach ($input_array as $rec) {
    $emails[] = $rec['email'];
}
$emails = implode(',', $emails);
Nathan
  • 1,700
  • 12
  • 15
  • I was looking for something like what William Okano suggested(without loop) but it's not compatible with PHP5.4. Thanks anyway – PersianHero Jul 14 '16 at 09:36
  • I tend to lean towards implementation which will run just about anywhere, but otherwise I agree `array_column()` would be the best option. – Nathan Jul 14 '16 at 13:36