Bricktop Posted July 21, 2009 Share Posted July 21, 2009 Hi all, I got some help earlier on here with some code which reads in a text file line by line and outputs the result. However, I would like to reverse the order of the output. I know I need to use array_reverse but not sure where to put it. Any help greatly appreciated. Thanks <?php $quotesfile = 'file.txt'; $content = ""; foreach(file($quotesfile) as $line) { list($quote,$author) = explode("||", $line, 2); //Display the quote and the author $content .= '<blockquote class="quotetext">'.$quote.'</blockquote><br /><br />'; $content .= '<cite class="authortext">'.$author.'</cite>'; } echo $content; ?> Link to comment https://forums.phpfreaks.com/topic/166832-solved-reverse-array/ Share on other sites More sharing options...
rhodesa Posted July 21, 2009 Share Posted July 21, 2009 that code looks familiar <?php $quotesfile = 'file.txt'; $lines = file($quotesfile); array_reverse($lines); $content = ""; foreach($lines as $line) { list($quote,$author) = explode("||", $line, 2); //Display the quote and the author $content .= '<blockquote class="quotetext">'.$quote.'</blockquote><br /><br />'; $content .= '<cite class="authortext">'.$author.'</cite>'; } echo $content; ?> Link to comment https://forums.phpfreaks.com/topic/166832-solved-reverse-array/#findComment-879718 Share on other sites More sharing options...
Bricktop Posted July 21, 2009 Author Share Posted July 21, 2009 lol, thanks again rhodesa, but it's still outputting in the same way. My code is: <?php $quotesfile = 'file.txt'; $lines = file($quotesfile); array_reverse($lines); $content = ""; foreach($lines as $line) { list($quote,$author) = explode("||", $line, 2); //Display the quote and the author $content .= '<blockquote class="quotetext">'.$quote.'</blockquote><br /><br />'; $content .= '<cite class="authortext">'.$author.'</cite>'; } echo $content; ?> Link to comment https://forums.phpfreaks.com/topic/166832-solved-reverse-array/#findComment-879723 Share on other sites More sharing options...
Bricktop Posted July 21, 2009 Author Share Posted July 21, 2009 Ah, I fixed it. Just added: $lines = array_reverse($lines); Thanks again rhodesa! Link to comment https://forums.phpfreaks.com/topic/166832-solved-reverse-array/#findComment-879735 Share on other sites More sharing options...
rhodesa Posted July 21, 2009 Share Posted July 21, 2009 yeah, my bad...i assumed array_reverse() worked like sort() you can also get rid of the $lines variable again: <?php $quotesfile = 'file.txt'; $content = ""; foreach(array_reverse(file($quotesfile)) as $line) { list($quote,$author) = explode("||", $line, 2); //Display the quote and the author $content .= '<blockquote class="quotetext">'.$quote.'</blockquote><br /><br />'; $content .= '<cite class="authortext">'.$author.'</cite>'; } echo $content; ?> Link to comment https://forums.phpfreaks.com/topic/166832-solved-reverse-array/#findComment-879737 Share on other sites More sharing options...
Bricktop Posted July 21, 2009 Author Share Posted July 21, 2009 Brilliant! Thanks so much! Link to comment https://forums.phpfreaks.com/topic/166832-solved-reverse-array/#findComment-879741 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.