I have two(2) .csv files as such:
CompanySubset.csv
| id | company | description |
|----|-----------|---------------------------|
| 1 | Apple | Description for Apple |
| 2 | Microsoft | Description for Microsoft |
| 3 | IBM | Description for IBM |
ContactSubset.csv
| id | name | address | phone |
|----|-------|-------------------|-------------|
| 1 | Bob | 1234 Address Lane | 1+234567890 |
| 2 | Sally | 4321 Address Lane | 1+987654321 |
| 3 | Cam | 2468 Address Lane | 1+468135901 |
Using php, I need to read each file and compare the id
column for matches between the two(2). If I find a matching id between the two(2), then I need to merge that specific row together.
Ex: CompanySubset
id
1 matches ContactSubset
id
1. Therefore, the row with an id
of 1 on ContactSubset
will be merged with row 1 on CompanySubset
. Which will then form the following:
| id | company | description | name | address | phone |
|----|---------|-----------------------|------|-------------------|-------------|
| 1 | Apple | Description for Apple | Bob | 1234 Address Lane | 1+234567890 |
I have been able to get each .csv into an array(see below), but thats about it.
$filename = 'CompanySubset.csv';
$company_array = [];
if (($handle = fopen("{$filename}", "r")) !== FALSE) {
while (($companyData = fgetcsv($handle, 1000, ",")) !== FALSE) {
$company_array[] = $companyData;
}
fclose($handle);
}