Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7065141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:55:13+00:00 2026-05-28T04:55:13+00:00

I have a csv file in a format resembling the following. There are no

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:55:13+00:00Added an answer on May 28, 2026 at 4:55 am
    const F_ID = 0;
    const F_USER = 1;
    const F_DATE = 2;
    const F_DESCRIPTION = 3;
    
    $array = array();
    
    if (($handle = fopen('test.csv', 'r')) !== FALSE) {
      while (($data = fgetcsv($handle, 1000, '|')) !== FALSE) {
    
        if (count($data) != 4)
          continue; //skip lines that have a different number of cells
    
        if (!array_key_exists($data[F_ID], $array)
          || strtotime($data[F_DATE]) > strtotime($array[$data[F_ID]][F_DATE]))
          $array[$data[F_ID]] = $data;
      }
    }
    

    You’ll have, in $array, what you want. You can write it using fputcsv.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CSV file which has the following format: id,case1,case2,case3 Here is a
I have a CSV file with date column in the following format mm/dd/yyyy (4/20/2012),
I have a .csv file and the format is shown below: mapping.csv 5188.40811,TMobileML 5131.40903,TMobileGregsapt
I have a CSV file in the format shown below, and I'm using the
I have a CSV file in the following format: id,code,date,address,complete_url Example data from the
I have a CSV file with the following format: ID | STUFF | Custom
I have a big csv files with the following format: CSV FILE 1 id,
I have csv files with the following format: CSV FILE a , b ,
I have a CSV file in following format which I wish to import in
If i have a CSV file that is in the following format fd!,sdf,dsfds,dsfd fd!,asdf,dsfds,dsfd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.