Echo the bottom line of a CSV

2019-09-19 07:12发布

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 />";
}
?>

标签: php csv echo
3条回答
时光不老,我们不散
2楼-- · 2019-09-19 07:57

Simple solution is.

$file = "file.csv";
$data = file($file);
$last_line = $data[count($data)-1];
查看更多
ら.Afraid
3楼-- · 2019-09-19 08:17

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);
?>
查看更多
虎瘦雄心在
4楼-- · 2019-09-19 08:17
<?php
$file = file("file.csv");
$last_line=count($file)-1;
echo $file[$last_line];
?>
查看更多
登录 后发表回答