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 7500893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:20:59+00:00 2026-05-29T20:20:59+00:00

I have a large 2 column csv file (data.csv) with a weeks worth of

  • 0

I have a large 2 column csv file (“data.csv”) with a weeks worth of data.
Column one has unix time stamps
Column two has strings:

1329548400,cats
1329548400,dogs
1329550200,fish
1329550200,cats
1329552000,dogs
1329552000,fish
1329584400,cats
1329584400,dogs
1329550200,cats

Via php, I need to convert data.csv into 7 files, Sunday.csv, Monday.csv…Saturday.csv

If I could grab each row, I know I can do something like

$temp = "data.csv";
$fileName = array_unique($temp);
foreach ($fileName as $row) {
    $theDay = date("Y", $row[firstcolumn]);
    if ($theDay == "Sunday") {
        $fileToWriteTo = "Sunday.csv";
        $f = fopen($fileToWriteTo, "a");
        fwrite($fileToWriteTo, $row . "\n");
        fclose($fileToWriteTo);
        }
    if ($theDay == "Monday") {
        $fileToWriteTo = "Monday.csv";
        $f = fopen($fileToWriteTo, "a");
        fwrite($fileToWriteTo, $row . "\n");
        fclose($fileToWriteTo);
        }
    }

Problem is

1) I don’t know how to read each row of data.csv, assuming the syntax above is wrong
2) I admit Im a bit of a php laymen 🙂

…how far off am I? Am I even in the ballpark?

  • 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-29T20:21:00+00:00Added an answer on May 29, 2026 at 8:21 pm

    At first, don’t open file at each iteration do it before loop.

    $fps = array(); // fps = File Pointers
    $days = array( 'Sunday', 'Monday', 'Tuesday', ...);
    foreach( $days as $key => $name){
        $fps[ $name] = fopen( 'a', $name . '.csv');
        if( !$fps[ $name]){
            die("Cannot open $name.csv");
        }
    
        // Although I wouldn't rely on date's `l` format, because
        // it can be localized and script may just stop working
        // and rather use:
        $fps[ $key] = fopen( 'a', $name . '.csv');
        if( $fps[ $key])...
        // Sunday = 7 = 0, Monday = 1...
    }
    

    Now, you’ll address fields via date( 'w'), which will give numbers 0 – Sunday, 6 – Saturday:

    $day = date( 'w', $timestamp);
    

    About making unique results… You’ll probably need to use function like in_array() and store all processed rows in one array.

    About reading and writing CSV files, there are 2 functions: fgetcsv() resp. fputcsv(). So entire loop:

    $temp = "data.csv";
    $rows = array(); // List of unique itemps
    $fp = fopen( $temp, 'r') or die( "Cannot open `$temp` for reading");
    // Put here loop which will open all 7 files as mentioned above
    // Optionally skip first row (it may contain header)
    fgets( $fp);
    
    // Read each row
    while( ($array = fgetcsv( $fp, 10000, ',', '"') !== false){
        // Check unique row
        $row = implode( ',', $array);
        if( in_array( $row, $rows)){
             continue; // Skip this iteration
        }
        $rows[] = $row;
    
        $day = date( 'w', $array[0]);
        fputcsv( $fps[$day], $array);
    }
    

    And don’t forget to close all files:

    fclose( $fp);
    foreach( $fps as $fp){
        fclose( $fp);
    }
    
    • 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 that has rows, where data for some columns only
I have a large CSV data file -- ~1,444,000 rows of data -- that
I have some large csv file, with about 200 header names (the first one
I have a large collection of data in an excel file (and csv files).
I have a large CSV file (5.4GB) of data. It's a table with 6
I have a relatively large csv/text data file (33mb) that I need to do
i have a large mysql database table in which one column contains values ranging
I have a large textfile, which has linebreaks at column 80 due to console
I have a fairly large Excel file. In this file there is a column
I have a very large CSV file that I imported into a sqlite table.

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.