[PHP] Re: =Next to last record in a file=

2002-10-08 Thread Erwin

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=rdbperiod=1site_no=01420500' 
);
array_pop( $content );
$current = array_pop( $content );
?

HTH
Erwin


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] Re: =Next to last record in a file=

2002-10-08 Thread David Robley

In article [EMAIL PROTECTED], [EMAIL PROTECTED] 
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=rdbperiod=1site_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=rdbperiod=1site_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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php