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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:58:56+00:00 2026-05-25T21:58:56+00:00

I have a csv file I need to cleanup. It contains 13 fields, but

  • 0

I have a csv file I need to cleanup. It contains 13 fields, but I only need 7 (Business, Address, City, St, Zip, Phone, Email)

I need to run through all of the records and create a new output of just the records with email addresses.

In nutshell… I load the original file, run the for loop, explode the results, then look for the records where the $tmp[10] index is not null. I then get the rest of the rest of required fields, and do a foreach loop and fwrite the results to a new csv file.

Depending on how I tweak the code, I get either…
A text file of just email addresses.
or
A text file of just the last record with an email address.

I have been working on this too long and I just need a fresh set of eyes to point out the problem. I am new to php, and want to make this work. Thanks on advance.

    <?php
    // See if file exists and is readable
    $file = 'uploads/AK_Accountants.csv';
    $newfile = basename($file,".csv");
    $newfile = $newfile.Date("Ymd").".csw";
    $fileNew = fopen('uploads/AK_'.Date("Ymd").'.csv','w+');
    // Read the file into an array called $sourcefile
    $sourcefile = file($file);
    // Loop through the array and process each line
    for ($i = 0; $i < count($sourcefile); $i++) {
        // Separate each element and store in a temp array
       $tmp = explode('","', $sourcefile[$i]);
        // Assign each element of the temp array to a named array key
        if ($tmp[10] != "") {
            $sourcefile[$i] = array('Business_Name' => $tmp[1], 'Address' => $tmp[3], 'City' =>  $tmp[4], 'State' => $tmp[5], 'Zip' => $tmp[6], 'Phone' => $tmp[7], 'Email' => $tmp[10]);
            foreach($sourcefile[$i] as $key => $value);
                fwrite($fileNew, $value);
        }
    }
    ?>
  • 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-25T21:58:57+00:00Added an answer on May 25, 2026 at 9:58 pm

    From a quick glance:

    foreach($sourcefile[$i] as $key => $value);
        fwrite($fileNew, $value);
    

    should be

    foreach($sourcefile[$i] as $key => $value){
        fwrite($fileNew, $value);
    }
    

    Also, you have

    $newfile = $newfile.Date("Ymd").".csw";
    

    rather than what I assume should be

    $newfile = $newfile.Date("Ymd").".csv";
    
    • 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 only 1 column, but has close to
I have a csv file contains values as: UID,NAME,USERNAME,ROLE,ADDRESS 12,Joe,joe_123,A,address1 13,Lee,lee_321,U,address2 I need to
I have a csv file that contains a location field. I need to import
I have csv file having some address data mostly in Finnish language. I need
I have a CSV file with names and addresses. I need to plot markers
I have a csv file with 48 columns of data. I need to open
I have a CSV file I'm importing but am running into an issue. The
I have csv file exported from access. (dates formatted to match mysql) I need
I have a CSV file with many rows in which I need to update/replace
I have a CSV file with around 1.8million rows. I need to insert them

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.