tags:

views:

217

answers:

7

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.

A: 

Once you are done reading the file into the array by a call to file. You can open the file for writing by using the fopen function, write into the file using the fwrite and close the file handle using fclose:

<?php
$file = file("states.txt");                  // read file into array.
$fh = fopen('states.txt','w') or die("..."); // open same file for writing.
sort($file);
for($i=0; $i<count($file); $i++)
{
    $states = explode(",", $file[$i]);
    echo $states[0], $states[1],"<br />";
    fwrite($fh,"$states[0],$states[1] <br />"); // write to file.
}
fclose($fh); // close file.
?>
codaddict
Thank you so much for the quick response. :D
arrgggg
By the way, I'm not on my computer, so I can't test it now, but I will later. Also, if I want to do this in descending order, I'm assuming that I can just change sort($file); to rsort($file); and that should work?
arrgggg
Yes, you need to use rsort($file) to sort the file contents in reverse order.
codaddict
Much appreciation. :D
arrgggg
Why the down vote ?
codaddict
If I did, my bad...but I don't think I did that. Anyhow it seems I need to fix from your example to get rid of "<br />" and insert "/n" since we're writing into a text file. Thanks.
arrgggg
Hmmm...I just clicked to vote up, but I need more reputation to do that. :(
arrgggg
@unicornaddict - Change your answer and I'll give you an up vote.
Brant
+1  A: 

Try using fopen and fwrite.

$fileWrite = fopen("filePah", "w");

for($i=0; $i<count($file); $i++)
{
    fWrite($fileWrite, $file[i]);
}
fClose($fileWrite);
CogitoErgoSum
Thank you too. :D
arrgggg
+1  A: 
<?php
$file = file("states.txt");
sort($file);
$newContent = "";
for($i=0; $i<count($file); $i++)
{
  $states = explode(",", $file[$i]);
  $newContent .= $states[0] .', '. $states[1] . PHP_EOL;
}

file_put_contents('states.txt',$newContent);
?>

PHP: file_put_contents

Brant
I've seend that function. I'll try that too. Thanks. :D
arrgggg
I also removed the "<br />" because in the .txt file you should use PHP_EOL ("\n" or "\r\n").
Brant
Ah, that's right. "<br />" is used for html. Thank you.
arrgggg
A: 

Open the file, write to it, close it (this assumes $file is the variable from your code):

$fp = fopen('states.txt', 'w');
for($i=0; $i<count($file); $i++)
    fwrite($fp, $file[$i]);
}
fclose($fp);

And see http://php.net/manual/en/function.fwrite.php

maligree
I've checked php.net, but they didn't show how to sort and write back into the same file. I've learned how to write to file, but I wasn't sure how to do both at the same time in one php script.
arrgggg
ummm ... open file for reading, read file, close file (you did all in one step with `file()`), sort, open file for writing, write to file, close file (the last three steps could be done via `file_put_contents()`, as Sebastian P. pointed out).
maligree
+3  A: 

The easiest way to write the contents of $file back to the file would be using file_put_contents in collaboration with implode.

file_put_contents("states.txt", implode($file));
Sebastian P.
+1, one-liner job
stereofrog
Sebastian, that was just perfect. All I had to change in that was to remove "\n" because it was adding extra blank line between each records. Thank you.
arrgggg
Oh, my bad. Fixed.Remember to accept it if you like it. :)
Sebastian P.
+1  A: 

Try something like this:

$fo = fopen("filename", "w");
$content = "";
for ($i = 0; $i < count($file); $i++) {
    $states = explode(",", $file[$i]);
    $content .= $states[0] . "," . $states[1] . "\n";
}
fwrite($fo, $content);
fclose($fo);
KotuS
But where's the sorting part..or do i just add that code followed by my code?
arrgggg
you just add your part before mine and it should work allright
KotuS
Thanks alot. I will try this also.
arrgggg
A: 

im trying to do a sort array what i'm i missing?

"; } fclose($file1);

?>

jon doe