Breaking 2 CSVs containing some identical data and insert into mysql database.

My PHP is a little broken at the moment, i'm still learning and not having much luck with this problem.

I have 2 files both CSV files containing data.

One contains data in this format
PLU1, Cola ,53, 25

The other one contains data in this format
Pepsi, Cola, PLU1

I want to end up with
PLU, Pepsi, Cola, 53, 25

I was thinking (Pseudo Code)

Load each line into an array of file 1
Load each line into an array of file 2

Compare array_file1[1] && array_file1[2] with array_file2[3] && array_file2[3] if they match, write the SQL Query to file, when complete write the file to data.sql ready to load into SQL when required.

I assume i would just write "array_file1[1], array_file2[1], array_file2[2], array_file1[3], array_file1[4] \n" to the file.

The problem i am having is looping the data to check ALL the files against ALL the others, and then Any that dont match having them put out separately, so i end up with 5 part CSV and 2 left overs files, of data that didnt match.

I must apologise that i dont have kind of working code, as i have no idea how to start this.

Any idea's ?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I think directly working with the SQL will give you far less of a headache:

Set up an SQL table (1) to house CSV file 1.
Set up an SQL table (2) to house CSV file 2.
Set up an SQL table (3) to house your combined data.
PHP script to import CSV file 1 to SQL table 1.
PHP script to import CSV file 2 to SQL table 2.
INSERT INTO table3 SELECT table1.field1, table2.field1, table1.field2, table1.field3, table1.field4 FROM table1, table2 WHERE table1.field1 = table2.field3

Note that I have assumed a typo in your combined dataset:

>I want to end up with
>PLU, Pepsi, Cola, 53, 25
PLU1, Pepsi, Cola, 52, 25
^ my assumption

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.