2

I am saving values with add_user_meta within the same meta key.

What I need is to separate them with commas when getting them with foreach so I can send them by wp_mail.

I need something like this:


1@gmail.com, 2@hotmail.com, 3@gmail.com

but when obtaining them directly, it returns them all together and glued.

some help?


$email = get_user_meta ($vendor_id, 'list_email', false);

foreach ($email as $emails) {
    echo $emails;
}

Result:


1@gmail.com2@hotmail.com3@gmail.com

I tried some options, but the comma in front must be from the second email to be readable in wp_mail and I don't understand how to do it

mario
  • 47
  • 6
  • Can you please do `print_r( $email );` before `foreach` and share the output? – Mohandes Nov 21 '21 at 06:51
  • yes, this is the result: Array ( [0] => 1@gmail.com [1] => 2@gmail.com [2] => 3@gmail.com [3] => 4@gmail.com [4] => 5@gmail.com [5] => 6@gmail.com [6] => 7@gmail.com [7] => 8@gmail.com [8] => 9@gmail.com [9] => 10@gmail.com ) – mario Nov 21 '21 at 06:55

2 Answers2

2

You just need to append the "," in your loop and then remove the last "," using PHP rtrim() function. This code will do what you want:

$email = ['1@gmail.com', '2@gmail.com', '3@gmail.com', '4@gmail.com'];
$output = '';
foreach ( $email as $emails ) {
  $output .= $emails . ', ';
}
$output = rtrim( $output, ', ' );
echo $output;

The output:

1@gmail.com, 2@gmail.com, 3@gmail.com, 4@gmail.com 

Edit: Thanks Vee for mentioning, there is way simpler solution for this that is Using implode() function, like this:

$email = ['1@gmail.com', '2@gmail.com', '3@gmail.com', '4@gmail.com'];
$output = implode(', ', $email);
echo $output;

Result:

1@gmail.com, 2@gmail.com, 3@gmail.com, 4@gmail.com

Tip: Always try to use proper names for your variables, here you should use $emails for the array of emails and $email for the single email item.

Mohandes
  • 452
  • 3
  • 15
0

If emails are array like this.

$emails = ['n1@email.tld', 'n2@email.tld', 'n3@email.tld'];

Use array_key_last().

if (! function_exists("array_key_last")) {
    /**
     * Get last array key for PHP version older than 7.3.
     *
     * @link https://www.php.net/manual/en/function.array-key-last.php#123016
     * @param array $array
     * @return int|string|null
     */
    function array_key_last($array) {
        if (!is_array($array) || empty($array)) {
            return NULL;
        }
       
        return array_keys($array)[count($array)-1];
    }
}
foreach ($emails as $index => $email) {
    echo $email;
    if (array_key_last($emails) !== $index) {
        echo ', ';
    }
}

Use implode().

echo implode(', ', $emails);

All of these result:

n1@email.tld, n2@email.tld, n3@email.tld

vee
  • 4,506
  • 5
  • 44
  • 81
  • Thank you very much for your answer, the previous one was useful and I marked it as valid anyway, thank you – mario Nov 21 '21 at 15:45
  • @mario No problem. The answer by Mohandes is working good. I'll up vote for that. – vee Nov 21 '21 at 15:49