0

I have an array $email like this:

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

I need this array to be in this format

'[email protected]', '[email protected]', '[email protected]' // RESULT EXPECTED

I am doing this to get my result:

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

But it results in this:

[email protected]@[email protected] // ACTUAL RESULT

What should i do get the result?

2
  • 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. Commented Sep 17, 2015 at 4:55
  • possible duplicate of php how to implode array with key and value without foreach Commented Sep 17, 2015 at 5:02

5 Answers 5

2

Try

$email = array(
    array('email_id' => '[email protected]'),
    array('email_id' => '[email protected]'),
    array('email_id' => '[email protected]'),
    array('email_id' => '[email protected]'),
    );

foreach($email as $id)
{
    echo "'".$id['email_id']."',";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Hassaan this works. but can it be like: '[email protected]', '[email protected]', '[email protected]' . As now it is coming as [email protected], [email protected], [email protected]. Thanks in advance
thanks. It gives result with a coma after last email also. like this: '[email protected]','[email protected]','[email protected]', which is wrong. I dont need comma at last.
1

I'm using Hassaan's technique :

$email = array(
    array('email_id' => '[email protected]'),
    array('email_id' => '[email protected]'),
    array('email_id' => '[email protected]'),
    array('email_id' => '[email protected]'),
);

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' => '[email protected]'),
    array('email_id' => '[email protected]'),
    array('email_id' => '[email protected]'),
    array('email_id' => '[email protected]')
);

echo implode(',', array_map(function ($entry) {
  return $entry['email_id'];
}, $input));

Comments

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);

Comments

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); 
?>

Comments

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) . "'";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.