Search code examples
phparraysemailvariablescarbon-copy

Convert PHP Array to a single Variable


What I'm trying to do is parse the CC addresses out of some mail headers using PHP, they come through as an array (oddly even if its just one address). And I'd like to just convert the array into a single long variable.

So for instance if I had the following array: array([email protected], [email protected]);

Then I want to convert that to a single variable that could be something like '[email protected],[email protected]'

I've tried several things and the main thing that I thought should have worked was the following:

$ccList[]=$headerinfo->cc;   

foreach( $ccList as $key=>$val ){
   $ccAddress .= $val.","; 
   }
Sys::log(LOG_ALERT,'CC Address is..'.$ccAddress);

but when I get that logfile it says "CC Address is...Array,"

Is there any way to accomplish what I'm wanting? I should note that as its CC addresses I won't always know if its 0 addresses or several or anywhere in between.

I've also tried a few things with print_r and var_dump but they didn't return the results I expected to see (email addresses). I think var_dump still showed "Array" (or nothing) and print_r just said "CC Address is ...1".

Any help is appreciated.


Solution

  • http://php.net/manual/en/function.implode.php take a look here.

    $newccAddress = implode(",", $ccAddress);