Jump to content

[SOLVED] Reverse array


Bricktop

Recommended Posts

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

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

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

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

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.