在sendmail的打印阵列 [英] Print array in sendmail

查看:138
本文介绍了在sendmail的打印阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想呼应sendmail的邮件正文中的数组。我创建了一个功能,打印POST数组:

I am trying to echo an array in sendmail's message body. I created a function to print a POST array:

function printArray($array, $pad=''){
     foreach (array_slice($array, 3) as $key => $value){
        echo $pad . "$key: $value <br>";
        if(is_array($value)){
            printArray($value, $pad.' ');
        }  
    } 
}

它打印出完美均通过的print_r

It prints perfectly both through print_r

printArray($_POST);

如果把一个变量

$Parray = printArray($_POST);
echo $Parray;

但我没有得到它在sendmail消息工作:

But I am not getting it work in sendmail message:

$message = printArray($_POST);
mail($to, $subject, print_r($message), $headers);

以上code消息中发送电子邮件,'1'。无法弄清楚我究竟做错了什么?由于阵呼应完美,它只是sendmail的不打印。

The above code sends email with '1' in message. Cant figure out what am I doing wrong here? since array echoes perfectly, its just the sendmail which does not print it.

推荐答案

您正在使用回声 printArray 功能,将数据发送到标准输出(所以你可以看到屏幕上的输出)。但是,如果你想使用的函数的结果在一个变量(如 $消息),你需要从你的函数返回值。

You are using echo in the printArray function, which sends the data to standard output (so you can see the output on your screen). But if you want to use the result of the function in a variable (like $message) you need to return the value from your function.

由于你的函数是递归一(它调用自己),你必须收集的函数调用的输出在一个局部变量,然后返回累计值。

Since your function is a recursive one (it calls itself), you'll have to collect the output of function calls in a local variable and then return the accumulated value.

所以我修改 printArray()函数返回构造消息作为字符串,打印在它旁边。事情是这样的:

So I'd modify the printArray() function to return the constructed message as a string, beside printing it. Something like this:

function printArray($array, $pad=''){
    $buffer = array();
     foreach (array_slice($array, 3) as $key => $value){
        $buffer[] = $pad . "$key: $value <br>";
        if(is_array($value)){
            $buffer[] = printArray($value, $pad.' ');
        }
    }
    $output = join('', $buffer);
    echo $output;
    return $output;
}

在其他的答案提到

此外,的print_r()将打印参数(相同样本 printArray()在问题的功能),但返回的值,除非你传递的第二个参数为真正,这将导致的print_r 返回字符串值。

Also as mentioned on other answers, print_r() will print the parameter (same as the sample printArray() function in the question) but return value is not the printed value unless you pass the second parameter as true, which causes print_r to return the string value.

这篇关于在sendmail的打印阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆