Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > CSV to associative arrays
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 19th November 04:40
rotron
External User
 
Posts: 1
Default CSV to associative arrays



Hi all,

Kind of a PHP newbie and was wondering if anyone could provide some
guidance. I have 2 CSV files with 2 columns each and want to find the
intersection of both based on the value in the first column. For
example

File 1

id1, apple
id2, orange
id3, banana
id4, durian
id5, tomato

File 2

id7, coke
id6, drpepper
id5, sprite
id4, 7up
id1, water

My desired output is:

id1, apple, water
id4, durian, 7up
id5, tomato, sprite

I've been trying to create some PHP scripts to do that, using merge,
usort, array_intersect. I think my main problem is that when use
fgetcsv, I'm creating multi-dimensional arrays.

I *think* what I should be doing is creating single dimension arrays
with IDs as keys, and the fruit/drink as values. Then I should be able
to use array_intersect_assoc to compare the keys from each list and
match IDs.

However, I don't know how to go from id1, apple to array(id1 =>
apple). Anyone know how I can read the CSV files in this way? Am i on
the right track?

Thanks!
  Reply With Quote


 


Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Php 5 forum, Apache forum, Iis forum, Functions forum, Classes forum, Librarys forum, Bugs forum, Postgres forum, Mysql forum, Paradox forum, Ms sql forum, Configurations forum, Php.ini forum, Problems forum, Scripting forum, Css forum, General forums, Off-topic talk, Links, Extra forums, Php


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666