I am trying to alphabetize then remove duplicates out of this array. Then I am trying to write each element in the array to its own line in a text file.
Meaning only one element of the sorted and unique array will be on a line.
But all I am getting is a blank text file.
How can I write the contents of this array, $sorted_lines to a text file so that each element is on a new line?
Here is my code:
<?php
$lines = file('american.txt');
$lines = array_walk($lines, 'trim');
$sorted_lines = sort(array_unique($lines));
$out = fopen("new.txt", "w");
foreach($sorted_lines as $line){
$line .= "\n";
fwrite($out, $line);
}
?>
if (fopen("new.txt", "w")) { //do work }Third, close the handle.fclose($out);