I have two CSVs:
1st csv is something like this:
number | color | size | animal
**1234** | black | big | cat
2nd csv is like this:
name | country | os | number | flavour | yesorno
john | world | windows | **1234** | good | yes
What I'm trying to do is to merge both CSV's (header titles and values of each row) based on matching number values:
number | color | size | animal | name | country | os | flavour | yesorno
**1234** | black | big | cat | john | world | windows | good | yes
I have been trying to use fgetcsv
and used keys but I am really a newbie to php and I do not know how to that. I need to understand the logic. Could anyone please help?
Many thanks.
-- edit for better udnerstanding -- based on another question on stackoverflow, I have tryed the following code, which is not working well. The two headers from both CSV files is not merged. It is also missing all the data from a csv, it only has one row of data merged.
Code was found in another question: Merging two csv files together using php and seemed like a perfect base for what I am trying to achieve. Unfortunately the outputed csv is malformed...
<?php
// 1st section
$fh = fopen('csv1.csv', 'r');
$fhg = fopen('csv2.csv', 'r');
while (($data = fgetcsv($fh, 0, ";")) !== FALSE) {
$csv1[] = $data;
}
while (($data = fgetcsv($fhg, 0, ",")) !== FALSE) {
$csv2[] = $data;
}
// 2nd section
for ($x = 0; $x < count($csv2data); $x++) {
if ($x == 0) {
unset($csv1data[0][17]);
$line[$x] = array_merge($csv2data[0], $csv1data[17]); //header
} else {
$deadlook = 0;
for ($y = 0; $y <= count($csv1data); $y++) {
if($csv1data[$y][17] == $csv2data[$x][0]){
unset($csv1data[$y][17]);
$line[$x]=array_merge($csv2data[$x],$csv1data[$y]);
$deadlook=1;
}
}
if ($deadlook == 0)
$line[$x] = $csv2data[$x];
}
}
// 3 section
$fp = fopen('final.csv', 'w'); //output file set here
foreach ($line as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
$fh = fopen('csv1', 'r');
$fhg = fopen('csv2', 'r');
while (($data = fgetcsv($fh, 0, ",")) !== FALSE) {
$csv1[]=$data;
}
while (($data = fgetcsv($fhg, 0, ",")) !== FALSE) {
$csv2[]=$data;
}
// 2nd section
for($x=0;$x< count($csv2);$x++)
{
if($x==0){
unset($csv1[0][0]);
$line[$x]=array_merge($csv2[0],$csv1[0]); //header
}
else{
$deadlook=0;
for($y=0;$y <= count($csv1);$y++)
{
if($csv1[$y][0] == $csv2[$x][0]){
unset($csv1[$y][0]);
$line[$x]=array_merge($csv2[$x],$csv1[$y]);
$deadlook=1;
}
}
if($deadlook==0)
$line[$x]=$csv2[$x];
}
}
// 3 section
$fp = fopen('final.csv', 'w');//output file set here
foreach ($line as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
In section 1
open those 2 file and put the content in an array $csv1[] and $csv2[], so $csv1[0] is the first line in csv1.csv, $csv1[1] is the second line in csv1.csv, etc, the same in $csv2[].
In section 2,
i compare the first word in every array, $csv1[x][0] and $csv2[x][0].
the first if, if($x==0), is to make the header. first, i delete the first word contractname using unset function and join $csv1[0] and $csv2[0] using array_merge function.
then, using those for i select the first word in every line from $csv2 array and compare with the first word from every line from $csv1 array. so, if($csv1[$y][0] == $csv2[$x][0]) check if those first word are the same, if those are the same string, delete it and merge those lines.
if those word arent the same, save the $csv2[x] line in $line array and continue.
In section 3,
save the content from $line array in the file.