CSV to associative arrays

This is a discussion on CSV to associative arrays within the PHP General forums, part of the PHP Programming Forums category; Hi all, Kind of a PHP newbie and was wondering if anyone could provide some guidance. I have 2 CSV ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-20-2008
rotron
 
Posts: n/a
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 Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:11 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0