Loop file from the end? [duplicate]












0
















This question already has an answer here:




  • Read a file backwards line by line using fseek

    7 answers




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











share|improve this question













marked as duplicate by RiggsFolly php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 15:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • I suppose you thought of doing a FOR loop from count($offers)-1 and decrementing the counter instead of incrementing it?

    – RiggsFolly
    Nov 13 '18 at 15:08











  • Of course if the file may become very large the DUP is probably the best solution

    – RiggsFolly
    Nov 13 '18 at 15:11











  • You could also say that the data should be in a database, there are a lot of possibilities.

    – Nigel Ren
    Nov 13 '18 at 15:12
















0
















This question already has an answer here:




  • Read a file backwards line by line using fseek

    7 answers




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











share|improve this question













marked as duplicate by RiggsFolly php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 15:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • I suppose you thought of doing a FOR loop from count($offers)-1 and decrementing the counter instead of incrementing it?

    – RiggsFolly
    Nov 13 '18 at 15:08











  • Of course if the file may become very large the DUP is probably the best solution

    – RiggsFolly
    Nov 13 '18 at 15:11











  • You could also say that the data should be in a database, there are a lot of possibilities.

    – Nigel Ren
    Nov 13 '18 at 15:12














0












0








0









This question already has an answer here:




  • Read a file backwards line by line using fseek

    7 answers




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











share|improve this question















This question already has an answer here:




  • Read a file backwards line by line using fseek

    7 answers




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






This question already has an answer here:




  • Read a file backwards line by line using fseek

    7 answers








php file foreach






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 15:05









XtremeXtreme

63661935




63661935




marked as duplicate by RiggsFolly php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 15:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by RiggsFolly php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 15:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • I suppose you thought of doing a FOR loop from count($offers)-1 and decrementing the counter instead of incrementing it?

    – RiggsFolly
    Nov 13 '18 at 15:08











  • Of course if the file may become very large the DUP is probably the best solution

    – RiggsFolly
    Nov 13 '18 at 15:11











  • You could also say that the data should be in a database, there are a lot of possibilities.

    – Nigel Ren
    Nov 13 '18 at 15:12



















  • I suppose you thought of doing a FOR loop from count($offers)-1 and decrementing the counter instead of incrementing it?

    – RiggsFolly
    Nov 13 '18 at 15:08











  • Of course if the file may become very large the DUP is probably the best solution

    – RiggsFolly
    Nov 13 '18 at 15:11











  • You could also say that the data should be in a database, there are a lot of possibilities.

    – Nigel Ren
    Nov 13 '18 at 15:12

















I suppose you thought of doing a FOR loop from count($offers)-1 and decrementing the counter instead of incrementing it?

– RiggsFolly
Nov 13 '18 at 15:08





I suppose you thought of doing a FOR loop from count($offers)-1 and decrementing the counter instead of incrementing it?

– RiggsFolly
Nov 13 '18 at 15:08













Of course if the file may become very large the DUP is probably the best solution

– RiggsFolly
Nov 13 '18 at 15:11





Of course if the file may become very large the DUP is probably the best solution

– RiggsFolly
Nov 13 '18 at 15:11













You could also say that the data should be in a database, there are a lot of possibilities.

– Nigel Ren
Nov 13 '18 at 15:12





You could also say that the data should be in a database, there are a lot of possibilities.

– Nigel Ren
Nov 13 '18 at 15:12












1 Answer
1






active

oldest

votes


















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.






share|improve this answer


























  • I suppose that array_reverse(array_reverse); should be array_reverse($offers)?

    – rpm192
    Nov 13 '18 at 15:09


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer


























  • I suppose that array_reverse(array_reverse); should be array_reverse($offers)?

    – rpm192
    Nov 13 '18 at 15:09
















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.






share|improve this answer


























  • I suppose that array_reverse(array_reverse); should be array_reverse($offers)?

    – rpm192
    Nov 13 '18 at 15:09














1












1








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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 15:10

























answered Nov 13 '18 at 15:07









Nigel RenNigel Ren

26.7k61833




26.7k61833













  • I suppose that array_reverse(array_reverse); should be array_reverse($offers)?

    – rpm192
    Nov 13 '18 at 15:09



















  • I suppose that array_reverse(array_reverse); should be array_reverse($offers)?

    – rpm192
    Nov 13 '18 at 15:09

















I suppose that array_reverse(array_reverse); should be array_reverse($offers)?

– rpm192
Nov 13 '18 at 15:09





I suppose that array_reverse(array_reverse); should be array_reverse($offers)?

– rpm192
Nov 13 '18 at 15:09



Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ