将PHP数组转换为CSV文件

将PHP数组转换为CSV文件,php,arrays,csv,Php,Arrays,Csv,我的问题是,我想将数组保存到CSV文件中。 这是我的代码: //Webseite auslesen $string = file_get_contents("***Zensiert***"); $helper = preg_match_all('!<a.*?href=\"([^\"]*)\"[^>]*>(.*?)</a>!', $string, $matches); $fl_array = preg_grep('/^http.*/', $matche

我的问题是,我想将数组保存到CSV文件中。 这是我的代码:

//Webseite auslesen
$string = file_get_contents("***Zensiert***");    
$helper = preg_match_all('!<a.*?href=\"([^\"]*)\"[^>]*>(.*?)</a>!', $string, $matches);    
$fl_array = preg_grep('/^http.*/', $matches[1]);
echo '<pre>';
print_r($fl_array);
echo '</pre>';

$fp = fopen('file.csv', 'a');

foreach ($fl_array as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
//Webseite-auslesen
$string=file\u get\u contents(“***Zensiert***”);
$helper=preg_match_all(“!!”,$string,$matches);
$fl_array=preg_grep('/^http.*/',$matches[1]);
回声';
打印(fl_阵列);
回声';
$fp=fopen('file.csv','a');
foreach($fl_数组作为$fields){
fputcsv($fp$fields);
}
fclose($fp);
文件已生成,但没有内容?! 我没有看到这个错误

希望你能帮助我

$fp = fopen('file.csv', 'w');
fputcsv($df, array_keys(reset($fl_array))); //Put column names as the 1st row (you may comment this line)
foreach ($fl_array as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);

$fields不是数组。。这就是问题所在。

我会将一些行更改为

这将打开要写入的文件


在代码中,您可能希望将foreach更改为:
fputcsv($fp,$fl\u数组)

fopen()
上尝试使用
w+
权限,其次,
$fl\u数组的实际内容是什么?我相信as
a
会将文件指针放在文件的末尾。见文件:
$fp = fopen('file.csv', 'w');
fputcsv($df, array_keys(reset($fl_array))); //Put column names as the 1st row (you may comment this line)
foreach ($fl_array as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);