我有一个这样的数组:
df['E'] = (df['A']>df['B']) & (df['B']< df['C']) & (df['B']>df['D'])
我想在php中从这个数组创建一个可下载的csv文件。 像这样:
unicode utf-8也很重要。
我找到了解决方案:
$data = [
["firstname" => "Mary", "lastname" => "Johnson", "age" => 25],
["firstname" => "Amanda", "lastname" => "Miller", "age" => 18],
["firstname" => "James", "lastname" => "Brown", "age" => 31],
["firstname" => "Patricia", "lastname" => "Williams", "age" => 7],
["firstname" => "Michael", "lastname" => "Davis", "age" => 43],
["firstname" => "Sarah", "lastname" => "Miller", "age" => 24],
["firstname" => "Patrick", "lastname" => "Miller", "age" => 27]
];
}
<?PHP
function cleanData(&$str)
{
if($str == 't') $str = 'TRUE';
if($str == 'f') $str = 'FALSE';
if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {
$str = "'$str";
}
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
$str = mb_convert_encoding($str, 'UTF-16LE', 'UTF-8');
它工作并创建.csv文件,但它显示波斯字符不可读。
答案 0 :(得分:0)
使用http://php.net/manual/en/function.fputcsv.php
$fp = fopen('php://output', 'w');
fputcsv($fp, array_keys($data[0]));
foreach ($data as $row) {
fputcsv($fp, array_values($row));
}
fclose($fp);
将数组转换为UTF8可以使用类似
的方法完成$dataUTF8 = array_map("utf8_encode", $data);