dpc57092 2014-05-02 11:00
浏览 29
已采纳

将PHP数组转换为CSV文件

my problem is, that I want to save an array into a CSV file. Here my code:

//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);

The file is generated, but without content?! I didn't see the mistake...

Hope you can help me.

  • 写回答

2条回答 默认 最新

  • dqkf36241 2014-05-02 11:05
    关注

    I would change some lines to those

    $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);
    

    This opens file to write.

    And in your code you probably want to change that foreach into: fputcsv($fp, $fl_array );

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?