php - sort the unsorted text file and rewrite to same text file in sorted order
- by arrgggg
Hi, I have a question. I am in process of learning how to read/write files, but having little trouble trying to do both at the same time in same php script. I have a text file with words like this,
Richmond,Virginia
Seattle,Washington
Los Angeles,California
Dallas,Texas
Jacksonville,Florida
I wrote a code to sort them in order and this will display in sort order by City.
<?php
$file = file("states.txt");
sort($file);
for($i=0; $i<count($file); $i++)
{
$states = explode(",", $file[$i]);
echo $states[0], $states[1],"<br />";
}
?>
From this, how can I rewrite those sorted information back into the states.txt file?
Thanks for your help.