[SOLVED] problem parsing a CSV file
Posted: Tue Oct 24, 2006 8:15 am
the file im parsing
the function im using to parse it
my output when using print_r
now as you can see for example on line 3
its turning the 2,697 into a 2, how can i get around this? i assumed that because it was in quotes this wouldnt happen, after all its a CSV file, im using a special CSV specific function and Excel reads it correctly
Code: Select all
Friday,1,"624","99","84","15"
Saturday,2,"1,123","164","146","18"
Sunday,3,"2,697","686","667","19"
Monday,4,"2,655","197","177","20"
Tuesday,5,"1,065","140","119","21"Code: Select all
function readCSV ($filePath) {
$row = 1;
$handle = fopen($filePath, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if (!isset($i)) {
$i = 1;
}
$num = count($data);
for ($c=0; $c < $num; $c++) {
if ($c == 0) {
$array[$i]['day'] = (string) $data[$c];
}
if ($c == 1) {
$array[$i]['date'] = (int) $data[$c];
}
if ($c == 2) {
$array[$i]['pageLoads'] = (int) $data[$c];
}
if ($c == 3) {
$array[$i]['unique'] = (int) $data[$c];
}
if ($c == 4) {
$array[$i]['firstTime'] = (int) $data[$c];
}
if ($c == 5) {
$array[$i]['returning'] = (int) $data[$c];
}
}
$i++;
}
fclose($handle);
return $array;
}Code: Select all
Array
(
[1] => Array
(
[day] => Friday
[date] => 1
[pageLoads] => 624
[unique] => 99
[firstTime] => 84
[returning] => 15
)
[2] => Array
(
[day] => Saturday
[date] => 2
[pageLoads] => 1
[unique] => 164
[firstTime] => 146
[returning] => 18
)
[3] => Array
(
[day] => Sunday
[date] => 3
[pageLoads] => 2
[unique] => 686
[firstTime] => 667
[returning] => 19
)
[4] => Array
(
[day] => Monday
[date] => 4
[pageLoads] => 2
[unique] => 197
[firstTime] => 177
[returning] => 20
)
[5] => Array
(
[day] => Tuesday
[date] => 5
[pageLoads] => 1
[unique] => 140
[firstTime] => 119Code: Select all
Sunday,3,"2,697","686","667","19" // the line in CSV file
// and now my output after doing print_r
[3] => Array
(
[day] => Sunday
[date] => 3
[pageLoads] => 2
[unique] => 686
[firstTime] => 667
[returning] => 19
)