0

I have an array $email like this:

Array
(
    [email_id] => bob2@example.com
)
Array
(
    [email_id] => bob3@example.com
)
Array
(
    [email_id] => bob4@example.com
)

I need this array to be in this format

'bob2@example.com', 'bob3@example.com', 'bob4@example.com' // RESULT EXPECTED

I am doing this to get my result:

$emails = implode(", " , $email);

But it results in this:

bob2@example.combob3@example.combob4@example.com // ACTUAL RESULT

What should i do get the result?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Aashi
  • 389
  • 1
  • 9
  • 20
  • 1
    Its difficult to use `implode` since all array keys are same. `implode` will work if the array is like `array('mail1', 'mail2')`. For the time being, use the solution put forward by @Hassan. – Tismon Varghese Sep 17 '15 at 04:55
  • possible duplicate of [php how to implode array with key and value without foreach](http://stackoverflow.com/questions/11427398/php-how-to-implode-array-with-key-and-value-without-foreach) – J Santosh Sep 17 '15 at 05:02

5 Answers5

2

Try

$email = array(
    array('email_id' => 'bob2@gmail.com'),
    array('email_id' => 'bob3@gmail.com'),
    array('email_id' => 'bob4@gmail.com'),
    array('email_id' => 'bob5@gmail.com'),
    );

foreach($email as $id)
{
    echo "'".$id['email_id']."',";
}
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
  • Thanks Hassaan this works. but can it be like: 'bob2@gmail.com', 'bob3@gmail.com', 'bob4@gmail.com' . As now it is coming as bob2@gmail.com, bob3@gmail.com, bob4@gmail.com. Thanks in advance – Aashi Sep 17 '15 at 05:00
  • thanks. It gives result with a coma after last email also. like this: 'bob2@gmail.com','bob3@gmail.com','bob4@gmail.com', which is wrong. I dont need comma at last. – Aashi Sep 17 '15 at 05:36
1

I'm using Hassaan's technique :

$email = array(
    array('email_id' => 'bob2@gmail.com'),
    array('email_id' => 'bob3@gmail.com'),
    array('email_id' => 'bob4@gmail.com'),
    array('email_id' => 'bob5@gmail.com'),
);

foreach($email as $id){
    $emails .= $id['email_id'].",";
}

$emails = substr($emails, 0, strlen($emails) -1 );

echo $emails;

With this technique you will not have the last comma.

Or you can use this technique that I found here

$input = array(
    array('email_id' => 'bob2@gmail.com'),
    array('email_id' => 'bob3@gmail.com'),
    array('email_id' => 'bob4@gmail.com'),
    array('email_id' => 'bob5@gmail.com')
);

echo implode(',', array_map(function ($entry) {
  return $entry['email_id'];
}, $input));
Community
  • 1
  • 1
Didier
  • 11
  • 4
0

Strange!! It should work.

How you have defined your $email array? Can you provide a code structure?

If you have something like this then it will definitely work.

$email = array('email1','email2');
echo implode(", ",$email);
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Nijesh Hirpara
  • 1,096
  • 1
  • 12
  • 17
0

You can try php's csv function for it

<?php

 $file = fopen("email.csv","w");

 foreach ($yourArray as $value)
   {
   fputcsv($file,explode(',',$value));
   }

 fclose($file); 
?>
Swapnil Bhikule
  • 606
  • 1
  • 8
  • 25
0

You could also use array_map to reduce the array of arrays into just an array of strings.

$actualEmails = array_map (function ($e) {
    return $e ['email_id'];
}, $email);
echo "'" . implode ("','", $actualEmails) . "'";
TreeTree
  • 3,200
  • 3
  • 27
  • 39