Read a csv file and convert

Hello,

I have a super easy question for you. It seems not difficult to do what I need but due to my poor experience in PHP and programmation, I am still struggling…

I would like to read a csv file and for specifics columns, convert some values and use the converted one. Ex: in the column Gender, I have Female or Male Or Unknown. When a value is Female, I would like to read it / convert it as 1, when a value is Male -> 2, and etc…

The csv file will contain unknown number of raws and columns…

Thank you very much for you help.

So easy, in fact, that you probably could have looked it up on the php site!

From http://php.net/manual/en/function.fgetcsv.php

[php]

<?php # Open the File. if (($handle = fopen("file.csv", "r")) !== FALSE) { # Set the parent multidimensional array key to 0. $nn = 0; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { # Count the total keys in the row. $c = count($data); # Populate the multidimensional array. for ($x=0;$x<$c;$x++) { $csvarray[$nn][$x] = $data[$x]; } $nn++; } # Close the File. fclose($handle); } # Print the contents of the multidimensional array. print_r($csvarray); ?>[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service