Scott Rowley Scott Rowley - 18 days ago 4
PHP Question

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(bob@example.com, bill@example.com);

Then I want to convert that to a single variable that could be something like
'bob@example.com,bill@example.com'

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.

Answer

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

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