severndigital Posted July 2, 2008 Share Posted July 2, 2008 ok .. so i have several csv files i want to read into an array to work with. my ideal solution would have the array look like this using print_r($array) Array(Line_1_field1 => Line_1_field2,Line_2_field1 => Line_2_field2,...)and so on. i can get the items out of the csv file and into an array but the array looks like this. Array ( [0] => Array ( [productid] => DOC001 [qty] => 1 ) [1] => Array ( [productid] => DOC002 [qty] => 5 ) which would work however I can't seem to figure out how to extract specifically the productid value and the qty value from this array. I know this is gonna be an easy one ... i just can't see it. Any suggestions. Thanks, -C here is the code i am using to produce the bottom result. $filename = 'testfile.csv'; $f = @fopen($filename,'r'); if (!$f) return false; $headers = fgetcsv($f,8090); $all = array(); while (!feof($f)){ $values = fgetcsv($f,8098); $row = array(); $i=0; if (is_array($values)){ foreach($values as $v){ if ($i < count($headers)) $row[$headers[$i]] = $v; $i++; } $all[] = $row; } } print_r($all); Link to comment https://forums.phpfreaks.com/topic/112921-solved-csv-file-to-array-got-myself-stuck/ Share on other sites More sharing options...
mbeals Posted July 2, 2008 Share Posted July 2, 2008 You need to reference it like: $all[$row_num][$key]; so the first product ID would be $all[0]['productid'] the second: $all[1]['productid'] does that help? Link to comment https://forums.phpfreaks.com/topic/112921-solved-csv-file-to-array-got-myself-stuck/#findComment-580050 Share on other sites More sharing options...
severndigital Posted July 2, 2008 Author Share Posted July 2, 2008 it's a point in the correct direction. i'll play around with it and let you know. Link to comment https://forums.phpfreaks.com/topic/112921-solved-csv-file-to-array-got-myself-stuck/#findComment-580057 Share on other sites More sharing options...
severndigital Posted July 2, 2008 Author Share Posted July 2, 2008 That worked swimmingly. Thanks, -C Link to comment https://forums.phpfreaks.com/topic/112921-solved-csv-file-to-array-got-myself-stuck/#findComment-580094 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.