0

so i have the following code:

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);
}

which produces the following in the txt file:

item1,element1,element2item2,element1,element2

if i try to use "\n" it produces this output:

item1,element1,element2
(blank line)
item2,element1,element2

how do i get it too:

item1,element1,element2
item2,element1,element2

This is the \n

function writetofile($myarray){
$outfile = "csv.txt"; 
$filewriter = fopen($outfile,"w"); // open for write
$item=$myarray[0]."\n".$myarray[1];
fwrite($filewriter, $item);

Thanks

user3329114
  • 13
  • 1
  • 6

2 Answers2

0

Try with the code below

<?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.'
');
}
Ankit Pise
  • 1,243
  • 11
  • 30
0

Why not like this?

<?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]);

  }

?>

The following code gives you (csv.txt) :

item1 ,element1 ,element2 
item2 ,element1 ,element2
Yuva Raj
  • 3,881
  • 1
  • 19
  • 30