Link to home
Start Free TrialLog in
Avatar of ramesh4046
ramesh4046

asked on

PHP - Open 2 CSV files and update the second CSV file

I have 2 huge CSV files. The first CSV file will look something like below.

jack   1 2 3 4
john   5 6 7 8
mark   8 7 9 10
mike   1 3 4 7
helen  3 4 5 6

Open in new window


The second CSV file will look something like below.

1 2  \t      12 23 45
3 5  \t      13 45 57
1 4  \t      12 98 88

Open in new window


The format of the second CSV file would be set of values (like 1,2 separated by spaces) and then a tab delimiter followed by values separated by spaces.

In the second CSV file all I need to do is, replace the values like 1,2,3 etc with the corresponding column values of the first CSV file. So, after replacing the values, the second CSV file should look like, For example, the first row in the second CSV file has 1 and 2 which corresponds to Jack and John in the first CSV file. So the final output that am expecting is something like this.

jack john     12 23 45
mark helen    13 45 57
jack mike    12 98 88

Open in new window


I was trying to use PHP and trying to do something like this.

$file = fopen("secondfile.csv","r");
//I am reading till tab space to get the values that needs replacement. 
while ($line = fgetcsv($file, 0, "\t") !== false)
  {
    //I should read till tab spce and replace it with the values from firstfile.csv
  }

fclose($file);
?>

Open in new window


I am not able to figure out a way on how to do this problem. Can someone please guide me in the right direction?
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CSV files contain data fields that are usually separated by commas, but there is a conspicuous lack of commas in the data samples.  Any chance we could see a link to the actual data or a subset of the actual data, so we can read the files from a data source?
I have to leave this question now.  This code tests out given the information in the OP.

http://www.laprbass.com/RAY_temp_ramesh4046.php

<?php // RAY_temp_ramesh4046.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28280262.html

// SIMULATE READING THESE FILES WITH file_get_contents()
$file1 = <<<EOD
jack   1 2 3 4
john   5 6 7 8
mark   8 7 9 10
mike   1 3 4 7
helen  3 4 5 6
EOD;

$file2 = <<<EOD
1 2  \t      12 23 45
3 5  \t      13 45 57
1 4  \t      12 98 88
EOD;

// TURN FILE 1 INTO A USEFUL DATA STRUCTURE
$data1    = array();
$data1[0] = NULL;
$arr = explode(PHP_EOL, $file1);
foreach ($arr as $str)
{
    // ARRAY NUMERIC INDEX WILL MATCH NUMBERS EXPECTED IN FILE 2
    $data1[] = current(explode(' ', $str));
}

// PROCESS FILE 2
$data3 = array();
$arr = explode(PHP_EOL, $file2);
foreach ($arr as $str)
{
    $out  = NULL;

    // SPLIT ON TAB AND SAVE THE RIGHTMOST DATA STRING
    $sub  = explode("\t", $str);
    $ends = trim(end($sub));

    // SPLIT ON BLANK
    $nums = trim($sub[0]);
    $nums = explode(' ', $nums);

    // USE INDEX NUMBER TO CHOOSE NAME
    foreach ($nums as $num)
    {
        $out .= $data1[$num] . ' ';
    }

    // RECOVER THE RIGHTMOST DATA STRING AND SAVE THE ELEMENT IN THE ARRAY
    $out .= $ends;
    $data3[] = $out;
}

// SHOW THE WORK PRODUCT
print_r($data3);

// WRITE: http://php.net/manual/en/function.file-put-contents.php

Open in new window

HTH, ~Ray