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

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动