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

    点赞 评论
  • drd43058 2014-05-02 11:04
    fputcsv($fp, $fields);
    

    $fields isn't an array.. so that was the problem.

    点赞 评论

相关推荐 更多相似问题