【发布时间】:2017-12-14 22:37:58
【问题描述】:
所以这可能很简单,但我似乎可以让它在我的最终工作。我目前有一个看起来像这样的嵌入式数组:
$array = array (
array(
$_SESSION['Contact']['Name'],
$_SESSION['Contact']['Email']
...
)
);
我的会话数组中有大约 30 个字段。使用上面的示例,我必须手动列出每个会话数组项。所以,我的问题是,有没有办法遍历 Sessions 数组以获得与上述相同的结果。
$array = array (
array(
foreach ($_SESSION as $key => $value){
$value.", ";
}
)
);
//我的电子邮件/CSV 功能
function create_csv_string($data) {
// Open temp file pointer
if (!$fp = fopen('php://temp', 'w+')) return FALSE;
// Loop data and write to file pointer
foreach ($data as $line) fputcsv($fp, $line);
// Place stream pointer at beginning
rewind($fp);
// Return the data
return stream_get_contents($fp);
}
function send_csv_mail ($csvData, $body, $to = 'xx@xxx.com', $subject = 'Test email with attachment', $from = 'zz@zzz.com') {
// This will provide plenty adequate entropy
$multipartSep = '-----'.md5(time()).'-----';
// Arrays are much more readable
$headers = array(
"From: $from",
"Reply-To: $from",
"Content-Type: multipart/mixed; boundary=\"$multipartSep\""
);
// Make the attachment
$attachment = chunk_split(base64_encode(create_csv_string($csvData)));
// Make the body of the message
$body = "--$multipartSep\r\n"
. "Content-Type: text/plain; charset=ISO-8859-1; format=flowed\r\n"
. "Content-Transfer-Encoding: 7bit\r\n"
. "\r\n"
. "$body\r\n"
. "--$multipartSep\r\n"
. "Content-Type: text/csv\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: attachment; filename=\"file.csv\"\r\n"
. "\r\n"
. "$attachment\r\n"
. "--$multipartSep--";
// Send the email, return the result
return @mail($to, $subject, $body, implode("\r\n", $headers));
}
send_csv_mail($array, "Hello World!!!\r\n This is simple text email message.");
【问题讨论】:
-
你为什么要这样掩埋会话数组?
-
我用它来创建一个 CSV 文件,然后将结果通过电子邮件发送给自己。
-
你不能只创建 CSV 而不必隐藏会话变量吗?
-
IDK,我是新手。我在上面粘贴了我的功能。我在网上找到了这个功能,它确实有效,所以我坚持使用它。
-
是嵌套数组的问题吗?您的示例显示了单个 $_SESSION['contact'],但是否还有 $_SESSION['contact2'] 和 $_SESSION['contact3'],每个都有成员 'Name' 和 'Email'?跨度>