-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 [email protected],[email protected],[email protected]

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

array(
        array('email' => '[email protected]'),
        array('email' => '[email protected]'),
        array('email' => '[email protected]'),

    )
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= "[email protected],[email protected],[email protected]";
$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] => [email protected]
        )

    [1] => Array
        (
            [email] => [email protected]
        )

    [2] => Array
        (
            [email] => [email protected]
        )

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