-2

I want to concatenate two CSV files:

url.csv :

www.text.com/test.html

www.text.com/test1.html

www.text.com/test2.html

color.csv :

Red

Black

Pink

result should like :

www.text.com/test.html?color=Red

www.text.com/test1.html?color=Red

www.text.com/test2.html?color=Red

www.text.com/test.html?color=Black

www.text.com/test1.html?color=Black

www.text.com/test2.html?color=Black

my final code :

$url_csv = "cache_build_urls_file.csv";
$url_csv_resource = fopen($url_csv,"r");

$color_csv = "colors_file.csv";
$color_csv_resource = fopen($color_csv,"r");

$new_color = "color_cache_build_urls_file.csv";
$outputBuffer = fopen($new_color, 'w');

$result = $urls = $colors = array();

if (($handle = fopen($url_csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $urls[] = $data[0];
}
fclose($handle);
}
if (($handle = fopen($color_csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $colors[] = $data[0];
 }
fclose($handle);
}
foreach( $urls as $url ) {
foreach( $colors as $color ) {
    $result[] = $url.'?color='.$color;

    }
}


foreach($result as $val) {
    $arr[0] = $val;
     fputcsv($outputBuffer,$arr);
}
fclose($outputBuffer);
Payal
  • 13
  • 8
  • 1
    show your code. what you tried yet and where you got stuck – Deep Kakkar Jun 12 '15 at 06:23
  • $url_csv = "cache_build_urls_file.csv"; $url_csv_resource = fopen($url_csv,"w"); $color_csv = "colors_file.csv"; $color_csv_resource = fopen($color_csv,"r"); $new_color = "new_color.csv"; while (($line = fgetcsv($url_csv_resource)) !== FALSE) { $color_line = fgetcsv($color_csv_resource)); // i dont know what to do here. how can i merge these two csv files } – Payal Jun 12 '15 at 06:40
  • @Payal Please add your code to the question itself. In the current form there are high chances your question will be rejected. – Arpith Jun 12 '15 at 06:42
  • @Payal : please edit your question and place the code there. Logis is simple to read two files and concatenate data as per your need. Finally put that in 3rd file (csv). – Pranay Bhardwaj Jun 12 '15 at 06:45
  • http://www.improgrammer.net/php-nested-loop/ – Joshua - Pendo Jun 12 '15 at 06:54
  • I don't see where the two files are CSV's. CSV stands for comma seperated values meaning you're seperating each cell / column by a comma and each new line is a new row. For me they look like plain text files. – Anticom Jun 12 '15 at 06:59

1 Answers1

1

I'm not going to explain everything to you, just some simple basics. What you're looking for is a nested loop. Basically you start with reading your first CSV file and for each result you get, you read the second file.

In case of large datasets in the CSV files I can recommend 'caching' the results first in to an array. This will speed op the final code. Here's a quick example:

$result = $urls = $colors = array();
if (($handle = fopen("source1.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $urls[] = $data[0];
    }
    fclose($handle);
}

I'm creating 2 arrays, one for URLs one for colors. Then I read the CSV file and populate the array. Same thing happens for the second source. Just figure out yourself what do to there.

Then you're going to create a nested loop that combines both arrays:

foreach( $urls as $url ) {
    foreach( $colors as $color ) {
        $result[] = $url.'?color='.$color;
    }
}

Now $result contains a combined list of the data in both CSVs. This $result array can be converted in to a CSV. There are plenty of examples available on SO: Convert array into csv

Community
  • 1
  • 1
Joshua - Pendo
  • 4,331
  • 6
  • 37
  • 51