0

I need to have a script that I can upload a CSV to and have it output the contents in a new order with some fields removed and some added. I have tried getting the csv into an array but emails seem to break it. An input might look like this:

col1, col2, col3, col4
name, addr, phon, emal
...

The output would need to be changed to:

col4, col1, col3, col5
emal, name, addr, stat
...

Notice col3 is gone and col5 is added. col5 is the same value for every row.

I was doing this with a foreach loop. Which worked but its kind of slow. I dont know if there is a better or faster way.

Edit:

The answer seems to work but it raises two other features that will help:

One, I have a field that includes a date and time like 1/31/15 23:59. is there an easy way to strip the last 6 characters?

Two, can we skip a line if say col3 has data? I have a refunded date column that I would like to skip over if theres a date in it. Its blank otherwise. I know I can use isset but I dont know how to make the loop skip and continue if col3 isset. Also I dont know if isset might still be TRUE if theres no data since the column still exists

2
  • 1
    Without code it is difficult to make suggestions. Commented Mar 8, 2015 at 2:28
  • The code I had didn't work at all and was not very good. I was hoping for something original from someone more experienced than me. Commented Mar 8, 2015 at 13:22

1 Answer 1

1
<?php
    $sInFile  = 'infile.csv';
    $sOutFile = 'outfile.csv';

    $iRow = 0;
    if( ( $rHandle = fopen( $sInFile, "r")) !== FALSE ) 
    {
        while( ( $aData = fgetcsv( $rHandle, 1000, ",") ) !== FALSE ) 
        {
            // Skip headers.
            if( $iRow != 0 )
            {
                $iCountColumns = count( $aData );
                for( $i = 0; $i < $iCountColumns; ++$i )
                {
                    // Source
                    // name, addr, phon, emal
                    $sTmp  = '';
                    $sTmp .= $aData[ 3 ];
                    $sTmp .= ', ';
                    $sTmp .= $aData[ 0 ];
                    $sTmp .= ', ';
                    $sTmp .= $aData[ 1 ];
                    $sTmp .= ', ';
                    $sTmp .= 'CONSTANT!'; 
                    $sTmp .= "\n"; 
                    // Output
                    // emal, name, addr, stat
                }
                file_put_contents( $sOutFile, $sTmp, FILE_APPEND | LOCK_EX );
            }
            ++$iRow;
        }
    }
?>
Sign up to request clarification or add additional context in comments.

4 Comments

If I set the input file as $_FILES['file']['tmp_name']; would it be able to directly access the upload or do I need to save it to a location and trigger this function?
$_FILES['file']['tmp_name']; will not give you a file location just the name. You need to move the file from it's temporary upload location to a specified directory and parse from there.
Ok, That seems okay, two new problems maybe you can solve. One, I have a field that includes a date and time like 1/31/15 23:59. is there an easy way to strip the last 6 characters? Two, can we skip a line if say col3 has data? I have a refunded date column that I would like to skip over if theres a date in it. Its blank otherwise. I know I can use isset but I dont know how to make the loop skip and continue if col3 isset. Also I dont know if isset might still be TRUE if theres no data since the column still exists.
use explode on data then set to first array key for date. $aBadData = explode( ' ', $sBadDate ); $sFixedData = $aBadDate[ 0 ]; Or use substr( $sBadDate, -6 ); To skip col3 just do a if( !empty( $aData[ 3 ] ) continue; to go to next loop. Avoid isset at all costs and realize that empty does empty and isset check together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.