将PHP数组转换为单个变量

时间:2022-10-22 23:59:22

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.

我想要做的是使用PHP从一些邮件头中解析CC地址,它们作为一个数组出现(奇怪的是即使只有一个地址)。我想将数组转换为单个长变量。

So for instance if I had the following array: array(bob@example.com, bill@example.com);

例如,如果我有以下数组: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'

然后我想将其转换为单个变量,可能是'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,"

但当我得到那个日志文件时,它说“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.

有没有办法完成我想要的东西?我应该注意到,作为其CC地址,我不会总是知道它的0地址或其中的几个地址。

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".

我也尝试过print_r和var_dump的一些东西但是它们没有返回我期望看到的结果(电子邮件地址)。我认为var_dump仍然显示“数组”(或没有),print_r只是说“CC地址是...... 1”。

Any help is appreciated.

任何帮助表示赞赏。

3 个解决方案

#1


2  

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

http://php.net/manual/en/function.implode.php看看这里。

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

#2


1  

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

您想使用implode函数。如,$ result = implode(',',array(bob @ example.com,bill @ example.com)),它将返回您的结果。

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

#3


0  

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

在此示例中,您可以使用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.

如果要谈到“将PHP数组转换为单个变量”,一般来说,您需要查看array_reduce()beast。

#1


2  

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

http://php.net/manual/en/function.implode.php看看这里。

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

#2


1  

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

您想使用implode函数。如,$ result = implode(',',array(bob @ example.com,bill @ example.com)),它将返回您的结果。

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

#3


0  

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

在此示例中,您可以使用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.

如果要谈到“将PHP数组转换为单个变量”,一般来说,您需要查看array_reduce()beast。