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

  • Home
  • SEARCH
  • 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 7959013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:30:26+00:00 2026-06-04T04:30:26+00:00

I have a csv file looks like this col1, col2, col3 1 , John,

  • 0

I have a csv file looks like this

col1, col2, col3
1 ,  John, ABC

I need to parse ABC in column 3 into A,B,C because the new relation needs col1, col3 (each letter).

$j is the row, and $i is the column.

$j = 0;  
while (($row = fgetcsv($handle, 1000, ',')) !== false) {
    $i = 0;
    $id = 0;
    foreach ($row as $field) {
        # save the id of each row
        if($j >0 && $i == 0){
            $id = $field;
        }

        # parse the third column for non-header rows)
        if($j > 0 && $i >= 3){
            $char_array = str_split($field);
            foreach($char_array as $value){
                //echo $value;
                //echo $id.$value."<br/>";
                mysql_query("INSERT INTO sample VALUES('".$id."', '".$value."')");
            }
        }
        $i = $i + 1;  # increment the column
    }
    $j = $j +1;   # move to the next row
}
  1. This is slow. I have more than 720 rows, and most of them have more than 5 characters in the third column. ABCDE, so on average we have 720 x 5. That’s a huge number.
    I get timeout. I can change the max time for execution locally, but I need to run this on my student linux account. I don’t have that kind of privilege.

  2. I suspect because of timeout (it didn’t say it this time), I have up id = 502. I am missing the rest (I have up to 720 rows).

What can I do? I am only interested in the first and third column.

  • 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-06-04T04:30:27+00:00Added an answer on June 4, 2026 at 4:30 am

    IO disk read/write is slow too. But it doesn’t timeout and with cache you are not going to perform that bad actually. In fact, I’ve done this all the time, with data of size larger than 20MB in PHP. Not that bad…. If I really want to do big data, I’d use Python, C++ algorithms because I have them ready anyway.

    Example, based on your code (might contain errors):

    $big = array(); 
    $j = 0;
    $k = 0;
    
    while(....) {
    
        ....
        foreach($char_array as $value){
            #mysql_query("INSERT INTO sample VALUES('".$id."', '".$value."')");
            $big[$k][0] = $id;
            $big[$k][1] = $value;
            $k = $k+1;
        }
    
      ....
    }
    

    Here you make a nested array. $big is the outer array, and inside you assign the column and row.

    Use fputcsv to write into a new csv. The new csv, according to your specification, should looks like this:

    1, A

    1, B

    and here is the code to write into csv (from php doc)

    $fp = fopen('new.csv', 'w');
    
    foreach ($big as $fields) {
        fputcsv($fp, $fields);
    }
    fclose($fp);
    
    }
    

    now you can load csv into db.


    EDIT.
    I just tried with 720 rows with data like this and copy and paste 720 times.

    1, ABCDEFGHIJK
    

    Didn’t take that long… maybe 1-2 seconds.

    • 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 looks like this $lines[0] = text, with commas,
So I have a CSV file that looks like this: 12345, Here is some
I have a CSV file containing some user data it looks like this: 10333,,an.10,Kenyata,,Aaron,,,,,,,,,,
I have a csv file that looks something like this (actual file has many
I have data stored in a csv file that looks like this: Date,BLOCK,,Wood,Miscellaneous,,Totals,MO Saturday,4055-RU,4055-AR,4091,1139,1158,,100
I have csv file with a line that looks something like this: ,,,,,,,,,, That's
I have a csv file that looks like this: 603629,0,ATLV0008,Vendor1,1942.60,11/04/2010,1942.60,9/1-9/30/10,EFT-JP 603627,2,ATLV0008,Vendor1,1242.40,11/04/2010,1242.40,7/1-7/31/10,EFT-JP 600023,0,FLD4V0003,Vendor2,1950.00,06/15/2010,1950.00,6/14/10 Request,EFT-JP 600024,0,FLD4V0003,Vendor2,1800.00,06/15/2010,1800.00,6/14/10
I have a CSV file that looks like this, where time is a UNIX
I have a csv file that looks like this: Please note, there are no
I have a CSV file that looks like this AS2345,ASDF1232, Mr. Plain Example, 110

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.