In PHP, how to sort a file in reverse order?

I have a text file (text.txt), for example:

shir beer geer deer 

I also have a php page with this source:

 <?php foreach (glob("*.txt") as $filename) { $file = $filename; $contents = file($file); $reverse = array_reverse($file, true); $string = implode("<br>" , $contents); echo $string; echo "<br></br>"; } ?> 

I want the php page to show:

 deer geer beer shir 

from the end of the file to the beginning.
thanks

+4
source share
2 answers

It looks like you are changing the file name, not the contents.

Do

 $reverse = array_reverse($content); // you can drop 2nd arg. $string = implode("<br>" , $reverse); 

instead

 $reverse = array_reverse($file, true); $string = implode("<br>" , $contents); 

You can also remove temporary variables from the script and do:

 foreach (glob("*.txt") as $filename) { echo implode("<br>" , array_reverse(file($filename))) . "<br></br>"; } 
+6
source
 <?php foreach (glob("*.txt") as $filename) { $file = $filename; $contents = file($file); $reverse = array_reverse($contents, true); $string = implode("<br>" , $reverse); echo $string; echo "<br></br>"; } ?> 

Your result was $ content, without converse.

+2
source

All Articles