elyfrank Posted September 4, 2010 Share Posted September 4, 2010 Hi guys, This is my problem: I have a csv file let's say have these values: blue,1,2,3,4 red,8,5,6,7 I need to add thes values into a 2d array to be able to use like this: colors[1][2]=2 colors[2][4]=7 Where my first number is the row and the second number is the value within that row. This is what I got so far <?PHP $i=0; $j=0; $file_handle = fopen("color-list.csv", "r"); while (!feof($file_handle) ) { $colors = fgetcsv($file_handle, 10000); for ($j=0; $j<=4; $j++) { $row[$i][$j]=$colors[$j]; print row[$i][$j]; } $i++; ?> For some reason this doesn't work. Maybe I am declaring the arrays wrong. Thank you! Link to comment https://forums.phpfreaks.com/topic/212516-extracting-data-from-csv-to-a-2d-array/ Share on other sites More sharing options...
sasa Posted September 4, 2010 Share Posted September 4, 2010 look fgetcsv() function Link to comment https://forums.phpfreaks.com/topic/212516-extracting-data-from-csv-to-a-2d-array/#findComment-1107180 Share on other sites More sharing options...
elyfrank Posted September 4, 2010 Author Share Posted September 4, 2010 I extracted the data fine with fgetcsv() function, the problem I am having is adding the values to the array so I can use them anyway I want. Link to comment https://forums.phpfreaks.com/topic/212516-extracting-data-from-csv-to-a-2d-array/#findComment-1107182 Share on other sites More sharing options...
sasa Posted September 4, 2010 Share Posted September 4, 2010 sorry. i didn't read your code try while($c = fgetcsv($file_handle, 10000)) $colors[] = $c; print_r($colors); Link to comment https://forums.phpfreaks.com/topic/212516-extracting-data-from-csv-to-a-2d-array/#findComment-1107187 Share on other sites More sharing options...
elyfrank Posted September 4, 2010 Author Share Posted September 4, 2010 Great, that's exactly what I was looking for. one more question: how do I echo does values without getting Array ( [0] (blue [1] => 0 [2] => 2 [3] => 2 [4]) etc etc I would like to print is just like this blue1234 Thanks Link to comment https://forums.phpfreaks.com/topic/212516-extracting-data-from-csv-to-a-2d-array/#findComment-1107215 Share on other sites More sharing options...
sasa Posted September 4, 2010 Share Posted September 4, 2010 foreach($colors as $c) echo implode('', $c), "<br />\n"; Link to comment https://forums.phpfreaks.com/topic/212516-extracting-data-from-csv-to-a-2d-array/#findComment-1107217 Share on other sites More sharing options...
elyfrank Posted September 5, 2010 Author Share Posted September 5, 2010 Hey sasa, How do I compare and sort this array? I am adding a new value at the end of the arrays like this $colors[$i][repeat]=$x; After I added the value I would like to compare and sort the array base on that value I have something like this so far and it is not working: function compare($x, $y) { if ( $colors[repeat] == $colors[repeat] ) return 0; else if ( $colors[repeat] < $colors[repeat] ) return -1; else return 1; } uasort($colors, 'compare'); print_r($colors); Link to comment https://forums.phpfreaks.com/topic/212516-extracting-data-from-csv-to-a-2d-array/#findComment-1107355 Share on other sites More sharing options...
sasa Posted September 5, 2010 Share Posted September 5, 2010 function compare($x, $y) { if ( $x['repeat'] == $y['repeat'] ) return 0; else if ( $x['repeat'] < $y['repeat'] ) return -1; else return 1; } uasort($colors, 'compare'); print_r($colors); Link to comment https://forums.phpfreaks.com/topic/212516-extracting-data-from-csv-to-a-2d-array/#findComment-1107403 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.