Convert PHP Array to a single Variable

7k views Asked by At

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.

3

There are 3 answers

4
onatm On BEST ANSWER

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

$newccAddress = implode(",", $ccAddress);
0
Brendon On

You want to use the implode function. As in, $result = implode(',', array([email protected], [email protected])), which would return your result.

http://php.net/manual/en/function.implode.php

0
pilat On

In this example, you can use implode(), off cause

If to speak about "Converting PHP Array to a single Variable" in general, it's where you'll want to take a look at array_reduce() beast.