douchen2011 2016-09-18 18:08
浏览 40
已采纳

是否可以使用File_put_contents($ fileName,$ array)以便数组中的每个项目都放在一个新行上?

I am trying to rewrite a file with the contexts of an array but I want each item in the array to be on separate line. Is this possible to do?

I currently have the following:

file_put_contents('file.txt',$tempArray); 

The array has the following contents:

$tempArray = ["xyz","zyx", "123"]

and I want the file to look like this in the end:

xyz
zyx
123

but I am getting it all on one line currently

  • 写回答

1条回答 默认 最新

  • doumin1897 2016-09-18 18:09
    关注

    Do file_put_contents('file.txt',implode(PHP_EOL,$tempArray)); and you are fine.

    • implode takes a seperator and array-values then concates them like: implode('+',array(1,2)) becomes 1+2

    • PHP_EOL is an predefined constant from php that defines " " on linux and " " on windows

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Keil编译时报错"no source": Error: #5: cannot open source
  • ¥50 操作系统时间无法更新
  • ¥20 Windows11, usb转hdmi,外接显示器无反应
  • ¥20 怎么在JavaFx的TableView中动态地添加数据。
  • ¥15 MFC里的工具栏按钮图标使用外部图片
  • ¥15 如何在 llama.cpp 服务器中实现用户登录功能的优化方案?(语言-c++)
  • ¥15 有会用octave 的吗,急!代做!好偿!
  • ¥15 有一套同城小程序源码,Uniapp前端,php+html+mysql后端 ,能不能教我搭建起来可以运行,我不知道怎样操作
  • ¥15 mac调用java.io接口无法在根目录生成文件
  • ¥15 java微服务节点假死,网关路由时长延迟