2

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
Scott Rowley
  • 486
  • 1
  • 7
  • 30

3 Answers3

2

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

$newccAddress = implode(",", $ccAddress);
onatm
  • 816
  • 1
  • 11
  • 29
  • When I echo key/val I get: Key is..0 Val is..Array but the output of your command is: "newCC Address outside loop is.." – Scott Rowley Oct 30 '11 at 02:49
  • adapt implode method to your case. implode makes a single variable as you wanted but you problem is way different than creating a single variable. make sure what you get from $headerinfo->cc; – onatm Oct 30 '11 at 03:03
  • Yea, what I don't understand is when I print $headerinfo->cc I get "Array" but when I print var_dump($headerinfo->cc) I get nothing. The issue is confused by this being done via a function so I have to print to a dashboard log just to see the results. Doesn't the output of "Array" suggest that its not empty though? – Scott Rowley Oct 30 '11 at 03:12
  • sigh, I went up further in my code and investigated imap_headerinfo. Turns out there is both cc and ccaddress that you can use. I just used ccaddress instead and its giving me exactly what I want. thanks Anyway guys, implode was what I needed otherwise! – Scott Rowley Oct 30 '11 at 03:16
1

You want to use the implode function. As in, $result = implode(',', array(bob@example.com, bill@example.com)), which would return your result.

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

Brendon
  • 879
  • 1
  • 6
  • 18
0

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.

pilat
  • 1,094
  • 1
  • 10
  • 17