dongwei3336 2014-03-11 08:42
浏览 70
已采纳

将每个php数组的内容写入文本文件中的新行

I have used a while loop to fetch rows from a database and collect them in an array.

Now what I want to achieve is that on each iteration of this loop, the elements of the array should be written on a new line in a text file

I've tried implode but it's not working. I am probably using it in a completely wrong way. Here is my code:

$query = "SELECT hist FROM loch ;";
$result = mysql_query($query) or die(mysql_error());

$file = "/opt/lampp/htdocs/rrugd/ip.txt";
fopen($file,'a');

$output=array();
while($row=mysql_fetch_row($result))
{

        $instr1 = implode("
", $row);
        file_put_contents($file, $instr1, FILE_APPEND | LOCK_EX);   
}

I want the output to look like this -

1 2 10 5
1 2 10

But what I am getting is this -

1 2 10 51 2 10

Where am I going wrong?

  • 写回答

1条回答 默认 最新

  • douqinlin5094 2014-03-11 08:43
    关注

    Use PHP_EOL to add a new line like so:

    file_put_contents($file, $instr1 . PHP_EOL, FILE_APPEND | LOCK_EX);   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题