Re: =Next to last record in a file=
| From: | Erwin | Date: | Tue, 08 Oct 2002 13:48:14 +0000 |
| Subject: | Re: =Next to last record in a file= | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-119258@lists.php.net to get a copy of this message | ||
Anthony Ritter wrote:
> I am trying to access a website that has tab delimited data and use
> PHP to open the file, read from the
> file for output.
>
> I'd like to then place this data into an array, loop through the
> array and output only the next to last record.
>
> Is there any way to loop through the following records - there are 96
> - and keep only the *next to last last one* (which would be the most
> current record) before hitting EOF.
>
> They store the records every 15 minutes for a 24 hour span.
>
> I do not need all 96 records - only the most current - or - next to
> last - record.
Read the file with the file function (http://www.php.net/file). This will
give you an array, with each element representing a line in the original
file.
Pop the last element away (http://www.php.net/array_pop) twice. The second
time you do this, store the element you will gain (pop will return the
element it popped). There you have it...
In code:
<?
$content = file(
'http://waterdata.usgs.gov/ny/nwis/uv?format=rdb&period=1&site_no=01420500'
);
array_pop( $content );
$current = array_pop( $content );
?>
HTH
Erwin