Re: =Next to last record in a file=
| From: | David Robley | Date: | Wed, 09 Oct 2002 03:28:52 +0000 |
| Subject: | Re: =Next to last record in a file= | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-119381@lists.php.net to get a copy of this message | ||
In article <20021008134816.24630.qmail@pb1.pair.com>, erwin@isiz.com
says...
> 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 );
> ?>
OK - this is a fine candidate for 'how many ways can it be done?'. My
contribution [NB untested]:
<?
$content = file(
'http://waterdata.usgs.gov/ny/nwis/uv?format=rdb&period=1&site_no=01420500
');
$current = $content{count($content)-2};
Use count to find the number of elements and use count-2 to select the
second-to-last element. Recall that the first element is [0].
Cheers
--
David Robley
Temporary Kiwi!
Quod subigo farinam