Echo the bottom line of a CSV

2019-09-19 08:10发布

问题:

I am attempting to echo the bottom line only of a .csv file. I echo the file in reverse order (as shown below) but I'm not sure how to only echo the top line of this. Please note that the csv variable, constantly changing.

Here is my php:

<?php
$file = file("file.csv");
$file = array_reverse($file);
foreach($file as $f){
    echo $f."<br />";
}
?>

回答1:

Simple solution is.

$file = "file.csv";
$data = file($file);
$last_line = $data[count($data)-1];


回答2:

Maybe try the end function see http://php.net/manual/en/function.end.php

<?php
$file = file("file.csv"); // You take care of reading all the lines.
$lastline = end($file);
?>


回答3:

<?php
$file = file("file.csv");
$last_line=count($file)-1;
echo $file[$last_line];
?>


标签: php csv echo