Adding carriage return to each line

634 views Asked by At

I have a result from a query which I am writing to a file. I want the file to list the rows vertically instead of horizontally. I have the following code:

$fh = fopen($file, 'w');
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
    $array[] = implode('|', $row);
}

foreach($array as $arr) {
    fputs($fh, $arr) . ',';
}

fclose($fh);

Ive tried replacing the comma with \r\n but the file still contains the horizontal representation of the data. BTW the ',' may not be in the correct place in the code.

1

There are 1 answers

0
sf_admin On

Put escape characters inside double quotes.

$fh = fopen($file, 'w');
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
    $array[] = implode('|', $row);
}

foreach($array as $arr) {
    fputs($fh, $arr."\r\n");
}

fclose($fh);