Humpty Posted April 3, 2009 Share Posted April 3, 2009 G'day Again all, I have firstly googled and searched this forum, but no resolution found. Trying to do: 1) Upload CSV (done) 2) Parse CSV 3) Set an Array Where the Keys are the values of the CSV header row 4) Assign the values of the following rows to a variable. 5) Update an MySQL Database ($SplitImportString[x] is the line I am dealing with from the CSV file...parsing it one line at a time) Example 1 (CSV): Col_1,Col_2,Col_3 Val_A1,Val_A2,Val_A3 Val_B1, Val_B2, Val_B4 Example 2 (HeaderArray) : $SplitHeaderRow = split(",", $SplitImportString); (this works fine) Now I want to create an array that uses those as keys. Below is an example of what I want in the array Example 3 (RowArray) : (Key = Value) Col_1 = Val_A1 Col_2 = Val_A2 Col_3 = Val_A3 I'm just not really sure what I need to do to make the KEYS of RowArray be the Values of HeaderArray. In the end i am will be performing the following SQL: "UPDATE `table` SET `" . $HeaderArray[0] . "` = '" . $RowArray[$HeaderArray[0]] . "',`" . $HeaderArray[1] . "` = '" . $RowArray[$HeaderArray[1]] . "' WHERE `" . $HeaderArray[0] . "` = '" . $RowArray[$HeaderArray(0)] . "' LIMIT 1" and again using the predifined values above "UPDATE `table` SET `Col_1` = 'Val_A1',`Col_2` = 'Val_A2' WHERE `Col_3` = 'Val_A3' LIMIT 1" I hope at the very least you can understand what I am trying to do and point in a better / different direction. Using in PHP 4.x I don't like using plugins and functions from online, nor 'toolboxes' as I don't like the idea that the development of them may stop. I am happy to use predifned php or MySQL functions, statements etc. Thanks for your help, let me know if you need further explanations. UPDATE: Sorry; The whole purpose is that the user will upload a CSV and as long as it has Headers I know what fields the user wants updated and each line will in turn store the values. The user has a need for MANY different updates via CSV and this seemed far easier / better / flexible than hard-coding for each need and then having to do it again if they find a new CSV 'layout' / 'order' for some other update. Quote Link to comment https://forums.phpfreaks.com/topic/152346-solved-array-from-csv-using-csv-headerrow-values-as-keys/ Share on other sites More sharing options...
sasa Posted April 3, 2009 Share Posted April 3, 2009 http://hr.php.net/manual/en/function.str-getcsv.php#85733 Quote Link to comment https://forums.phpfreaks.com/topic/152346-solved-array-from-csv-using-csv-headerrow-values-as-keys/#findComment-800107 Share on other sites More sharing options...
Humpty Posted April 3, 2009 Author Share Posted April 3, 2009 looked shorter than what I was doing. Will check it out, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/152346-solved-array-from-csv-using-csv-headerrow-values-as-keys/#findComment-800108 Share on other sites More sharing options...
Humpty Posted April 13, 2009 Author Share Posted April 13, 2009 sorry it took so long to reply. fgetcsv was the answer. I had no issues with it based on the examples provided. I had to do some funky coding with array_flip for the header rows so that I could get the index # for the items i needed as a 'key' if you will. I also found complatibility issues is the CSV was saved via MS Office for Mac. I'm sure there would be ways around it but in this scenario there is no need for that. The code is a little lengthy so I won't paste it in here, however if someone needs some help with the array_flip method that I used then by all means contact me via a PM to notify me of your post and I will get back to you in your post. THANKS FOR THE HELP AND GUIDANCE guys ! Quote Link to comment https://forums.phpfreaks.com/topic/152346-solved-array-from-csv-using-csv-headerrow-values-as-keys/#findComment-808520 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.