0

I have a text file like this

2018-10-09 1

2018-11-12 1

2018-11-13 7

2018-11-15 1

2018-11-18 7

I loop the file to display the result like this

<?php
    $offers = file('logs/offer.txt');
    foreach($offers as $line) {
        $lineArray = explode("\t", $line);
        list($date, $quantity) = $lineArray;
        echo '<tr>      
        <td>' . $date . '</td>
        <td>' .$quantity. '</td>
        </tr>';
    }
?>

I get the result but I want to start from the end of the file and echo the result like this (start with latest date)

2018-11-18 7

2018-11-15 1

2018-11-13 7

2018-11-12 1

2018-10-09 1

Xtreme
  • 1,601
  • 7
  • 27
  • 59

1 Answers1

1

You can easily reverse the file contents array using array_reverse() before using foreach...

$offers = file('logs/offer.txt', FILE_IGNORE_NEW_LINES);
$offers = array_reverse($offers);
foreach($offers as $line) {

Note I've also added FILE_IGNORE_NEW_LINES which will take the new line off the end of each row, you can remove it if required.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55