1

Well, it seems strange that no one has asked this question before and it has been bothering me for hours but here's what I am trying to do.

I need to reverse the order of a text file, show the last 10 entries and print the contents but not from the last entry showing first. Rather I wish to maintain the order. ie. the first entry should show first and the last entry should show last.

I already have the PHP code to reverse and show the last 10 entries which I posted below with the help of a user. Thanks a ton for your help in advance!

Code

<?php $text = file('user.txt');
$text= array_reverse($text);
$counter=0; 
while ($counter < 10) { 
   if (isset($text[$counter])) { 
       echo $text[$counter] . "";
   } 
   $counter++;
} 
?>
Rugmangathan
  • 3,186
  • 6
  • 33
  • 44
enoma9
  • 33
  • 1
  • 7
  • You could paste it here itself or upload it in ideone.com and post the link here. – a3.14_Infinity Nov 05 '14 at 09:30
  • Are you doing this in a programming language? – David Hedlund Nov 05 '14 at 09:31
  • it's PHP code above...works like a charm but shows the last of the 10 entries first. I need to change the code to show the first of the last 10 entries first. For example if the text file contains...mango as last entry and blue as second last entry then I wish to show blue first and mango second thereby preserving the order of the last 10 lines. – enoma9 Nov 05 '14 at 09:32
  • http://stackoverflow.com/questions/15025875/what-is-the-best-way-in-php-to-read-last-lines-from-a-file – Flosculus Nov 05 '14 at 09:36

2 Answers2

0

Hope the following code will work for you :

                   $text = file('user.txt'); 
                   $total_line=count($text);
                   $start=$total_line-10;
                   for($i=$start;$i<=$total_line;$i++)
                   {
                          echo $text[$i].'<br>';
                   }

Please let me know if you have any issues with the above code.

Tristup
  • 3,603
  • 1
  • 14
  • 26
  • Thanks for your code. It works fine but I feel the earlier one is slightly better. – enoma9 Nov 05 '14 at 15:32
  • enoma thanks for your reply, but if you gone through the code which you have selected it used much of predefined function which cause the memory uses . – Tristup Nov 07 '14 at 10:01
0

You are almost there:

$newArray = array_reverse(array_slice($text, 0, 10));
var_dump($newArray);

array_slice get the first 10(last 10 before reverse), and then you reverse the array to get its original order.

Complete code:

$text = file('user.txt');
$text= array_reverse($text);
$newArray = array_reverse(array_slice($text, 0, 10));
foreach($newArray as $value) 
  printf("<pre>%s</pre>", $value);

If you don't need the complete array in reverse, you can only fetch what you really need:

$text = file('user.txt');
$newArray = array_slice($text, count($array)-10);
foreach($newArray as $value) 
      printf("<pre>%s</pre>", $value);
msfoster
  • 2,474
  • 1
  • 17
  • 19
  • Thanks so much, really appreciate you helping me out with this. I was going crazy on this one. – enoma9 Nov 05 '14 at 15:35