duanquan1876 2016-04-30 11:28
浏览 131

在foreach循环PHP中的Fwrite

I want to write the contents of an array to new file.

So far as it stands my file is only containing the last element of my array and not the first two. So the text in the file output2.txt is Edward only.

Have I misunderstood something here?

$array = array ("Sarah", "William", "Edward");

foreach ($array as $value) {
    $myfile = fopen("output2.txt", "w") or die("Unable to open file!");
    fwrite($myfile,$value);
    fclose($myfile);
} 
  • 写回答

2条回答 默认 最新

  • drjyvoi734793 2016-04-30 11:32
    关注
    $myfile = fopen("output2.txt", "w") or die("Unable to open file!");
    $abcd = "";
    foreach ($array as $value) {
        $abcd .= $value;       
    } 
    
    fwrite($myfile,$abcd);
    fclose($myfile);
    

    you are creating new file each time try this may help you

    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏