I have a csv file in a format resembling the following. There are
no column heads in the actual file – They are shown here for clarity.
id|user|date|description
0123456789|115|2011-10-12:14:29|bar rafael
0123456789|110|2012-01-10:01:34|bar rafael
0123456902|120|2011-01-10:14:55|foo fighter
0123456902|152|2012-01-05:07:17|foo fighter
0123456902|131|2011-11-21:19:48|foo fighter
For each ID, I need to keep the most recent record only, and write
the results back to the file.
The result should be:
0123456789|110|2012-01-10:01:34|bar rafael
0123456902|152|2012-01-05:07:17|foo fighter
I have looked at the array functions and don’t see anything that
will do this without some kind of nested loop.
Is there a better way?
You’ll have, in
$array, what you want. You can write it usingfputcsv.NOTE. I didn’t test this code, it’s meant to provide a basic idea of how this would work.
The idea is to store the rows you want into
$array, using the first value (ID) as the key. This way, on each line you read, you can check if you already have a record with that ID, and only replace it if the date is more recent.