所以我有以下代码:
myarray[1] = item1, element1, element2
myarray[2] = item2, element1, element2
$outfile = "csv.txt";
$filewriter = fopen($outfile, "w"); // open for write
foreach($myarray as $item) {
fwrite($filewriter, $item);
}
在txt文件中生成以下内容:
ITEM1,元素1,element2item2,元素1,element2的
如果我尝试使用“\ n”,它会产生此输出:
item1,element1,element2
(blank line)
item2,element1,element2
我如何得到它:
item1,element1,element2
item2,element1,element2
这是\ n
function writetofile($myarray){
$outfile = "csv.txt";
$filewriter = fopen($outfile,"w"); // open for write
$item=$myarray[0]."\n".$myarray[1];
fwrite($filewriter, $item);
由于
答案 0 :(得分:0)
尝试使用以下代码
<?php
$myarray[1] = 'item1, element1, element2';
$myarray[2] = 'item2, element1, element2';
$outfile = "csv.txt";
$filewriter = fopen($outfile, "w"); // open for write
foreach($myarray as $item) {
fwrite($filewriter, $item.'
');
}
答案 1 :(得分:0)
为什么不喜欢这个?
<?php
$myarray = array("item1 ,", "element1 ,", "element2 \r\n","item2 ,","element1 ,","element2");
$array_length = count($myarray); // Array length
$outfile = "csv.txt";
$filewriter = fopen($outfile, "w");
for($x=0;$x<$array_length;$x++)
{
echo $myarray[$x];
fwrite($filewriter, $myarray[$x]);
}
?>
以下代码为您提供(csv.txt
):
item1 ,element1 ,element2
item2 ,element1 ,element2