Jump to content

[SOLVED] Help sorting words from a file.


sh4d0w

Recommended Posts

Hi!

I'm doing a script that has to sort all the word on a file

but I'm having some troubles....

how can i store the words from the file on an array and then

after sorting them passing it back to the file in order

or just sort it from the file directly???

if anyone can it would really help me out

thanks

 

each line on the file has just one word its sorta like a word list.

 

also sorry for my first post being a question

i promise i'll help out with anything i can.

Link to comment
https://forums.phpfreaks.com/topic/135175-solved-help-sorting-words-from-a-file/
Share on other sites

Try something to this effect, I'm not providing all the code just yet, it'd be best if you googled it

 

//Read the file into $Contents (is fread() the right syntax?)

$Contents = split(" ", $Contents);
$New = "";
foreach ($Contents as $v)
{
$New =. $v . "\n\r";
}

//Write $New into the file (fwrite()?)

The file() function reads the entirety of the specified file into an array, one element for each line.

You can then use normal array sorting functions to sort the array however you wish

Then just loop through the array writing each element back to the file

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.