-2

I have string separated values and I am trying to reformat them in a specific format.

$ids = $_REQUEST('ids'); // ids is comma separated string
// ids looks like abc@gmail.com,def@gmail.com,xyz@gmail.com

For Google calendar api, I want it in this format:

array(
        array('email' => 'abc@gmail.com'),
        array('email' => 'def@gmail.com'),
        array('email' => 'xyz@gmail.com'),

    )
Ashish
  • 31
  • 2
  • 7

1 Answers1

2

The requirement is quite simple:

  1. Explode the string by comma.

  2. Get Email addresses

  3. Append them to array by adding email as a key.

  4. Done.

You can do it with PHP's explode() function:

<?php
$emailStr= "abc@gmail.com,def@gmail.com,xyz@gmail.com";
$emails = explode(',', $emailStr);
//echo '<pre>';print_r($emails);echo '</pre>';
$gmails = [];
if (! empty($emails)) {
    foreach ($emails as $email) {
        $gmails[] = ['email' => $email];
    }
}

echo '<pre>';print_r($gmails);echo '</pre>';

Output:

Array
(
    [0] => Array
        (
            [email] => abc@gmail.com
        )

    [1] => Array
        (
            [email] => def@gmail.com
        )

    [2] => Array
        (
            [email] => xyz@gmail.com
        )

)
Pupil
  • 23,834
  • 6
  • 44
  • 66