Reignfire Posted May 6, 2014 Share Posted May 6, 2014 Hi, I have a csv-file with data like this: I have a mysql database with all the names of the ingredients and a webpage with a searchbox so for example if i search for apple select it and then i search and select blueberry i want to get the corresponding data for apple and blueberry ( -4 in the example ). I know i can read the csv-file with 'fgetcsv' but i'm not sure how to import it into an array so it's easy to use so i can search for ingredients. Does anybody have an idea how to do this in php? Quote Link to comment https://forums.phpfreaks.com/topic/288273-importing-matrix-csv-file-into-array/ Share on other sites More sharing options...
Solution Ch0cu3r Posted May 6, 2014 Solution Share Posted May 6, 2014 I know i can read the csv-file with 'fgetcsv' but i'm not sure how to import it into an array so it's easy to use so i can search for ingredients. I'd parse the data into a multidimensional array. So the row heading, will be used as the array key for the array containing the row data. Id assign each item in the row array with the corresponding column heading as the key. Example array structure Array ( //row 1 heading [apple] => Array ( // row data [apple] => 3 // column 1 value [blueberry] => -4 // column 2 value [carrot] => -1 [date] => 0.0002 [egg] => 0 [fish] => 0.0003 [garlic] => 0.006 [herring] => 1.00 [jasmin] => 9.62E-06 ) //column 2 heading [blueberry] => Array ( // row data [apple] => 0 [blueberry] => 0 [carrot] => 0 [date] => 0 [egg] => 0 [fish] => 0 [garlic] => 0 [herring] => 0 [jasmin] => 0 ) ...etc } To generate that array structure you'd use $handle = fopen('test.csv', "r"); // first row of the csv file contains the column headings $headings = fgetcsv($handle, 0, ","); $headings = array_filter($headings); // parse the the values into an array $data = array(); while (($row = fgetcsv($handle, 0, ",")) !== false) { // remove the row label from the row, use it as the arrat key later $row_label = array_shift($row); // set the corresponding heading as the key for the values in the row. $data[$row_label] = array_combine($headings, $row); } Now you can now easily get the intersecting value using $data[ $row_label ][ $column_label ] echo $data['apple']['blueberry']; // returns -4 Quote Link to comment https://forums.phpfreaks.com/topic/288273-importing-matrix-csv-file-into-array/#findComment-1478387 Share on other sites More sharing options...
Jacques1 Posted May 6, 2014 Share Posted May 6, 2014 Hi, is there any reason why you want to load the data into an array? Parsing the entire CSV an every single request is pretty much the most cumbersome and inefficient solution you can get, especially when you're dealing with more data than in your example. It would make much more sense to import the data into your database where it belongs. Then you can actually query it and fetch specific information. Quote Link to comment https://forums.phpfreaks.com/topic/288273-importing-matrix-csv-file-into-array/#findComment-1478390 Share on other sites More sharing options...
Reignfire Posted May 7, 2014 Author Share Posted May 7, 2014 is there any reason why you want to load the data into an array? The file i got wasn't the final version so i thought that maybe it would be easier to load the data into an array instead of importing it into the database and having to empty and refill the database everytime i got a new file. But the performance was indeed more important so i parsed the file into the database. Thanks for both replies! Quote Link to comment https://forums.phpfreaks.com/topic/288273-importing-matrix-csv-file-into-array/#findComment-1478539 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.