Page 1 of 1

[SOLVED] problem parsing a CSV file

Posted: Tue Oct 24, 2006 8:15 am
by malcolmboston
the file im parsing

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"
the function im using to parse it

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;
}
my output when using print_r

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] => 119
now as you can see for example on line 3

Code: 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
        )
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

Posted: Tue Oct 24, 2006 8:19 am
by printf
This line...

Code: Select all

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
Should include the param => enclosure

Code: Select all

while ( ( $data = fgetcsv ( $handle, 1000, ',', '"' ) ) !== false ) {

pif!

Posted: Tue Oct 24, 2006 8:20 am
by volka
1,123: is this supposed to be 1123 or 1+(123/1000) ?
You're casting the value to (int) and php doesn't recognize thousands-separators.

Posted: Tue Oct 24, 2006 8:22 am
by malcolmboston
using

Code: Select all

while (($data = fgetcsv ($handle, 1000, ',', '"')) !== false ) {
same result

Posted: Tue Oct 24, 2006 8:25 am
by JayBird
What version of PHP you using?

The fourth param once introduced in 4.3.0

Posted: Tue Oct 24, 2006 8:29 am
by volka
excuse me that I insist but the problem is somewhere else

Code: Select all

$a = '1,123';
echo (int)$a;
The output is 1
That's also true for

Code: Select all

<?php
$a = '1,123';
echo (float)$a;
?>
but

Code: Select all

<?php
$a = '1.123';
echo (float)$a;
?>
prints 1.123

Posted: Tue Oct 24, 2006 8:32 am
by printf
Ok, I see what is wrong, you can't CAST to INT, leave it as a string

<?php

$number = (int) '1,9999';

echo $number;

?>

remove the (int)...

Code: Select all

$array[$i]['pageLoads'] = $data[$c];

pif!

Posted: Tue Oct 24, 2006 8:36 am
by malcolmboston
PHP 4.4.0 on Windows XP, Apache 2.0

using this function to parse the CSV file

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];
         } elseif ($c == 1) {
            $array[$i]['date'] = (float) $data[$c];
         } elseif ($c == 2) {
            $array[$i]['pageLoads'] = (float) $data[$c];
         } elseif ($c == 3) {
            $array[$i]['unique'] = (float) $data[$c];
         } elseif ($c == 4) {
            $array[$i]['firstTime'] = (float) $data[$c];
         } elseif ($c == 5) {
            $array[$i]['returning'] = (float) $data[$c];
         } else {
            // Impossible
            print "<h1>Serious Malfunction</h1>";
         }
      }
      $i++;
   }
   fclose($handle);
   return $array;
}
produces this...

Code: Select all

// var dump
[4]=>
  array(6) {
    ["day"]=>
    string(6) "Monday"
    ["date"]=>
    float(4)
    ["pageLoads"]=>
    float(2)
    ["unique"]=>
    float(197)
    ["firstTime"]=>
    float(177)
    ["returning"]=>
    float(20)
  }
for this

Code: Select all

Monday,4,"2,655","197","177","20"
that is dumped as soon as i receive the array

Posted: Tue Oct 24, 2006 8:37 am
by malcolmboston
printf wrote:Ok, I see what is wrong, you can't CAST to INT, leave it as a string

remove the (int)...

pif!
i plan on doing some rather heavy mathemtical work on these figures later on.......shouldnt they be numeric?

Posted: Tue Oct 24, 2006 8:48 am
by malcolmboston
never mind, solved

kept as a string, str_replace (",", "", $blah);

works a charm

Posted: Tue Oct 24, 2006 9:00 am
by printf
then just remove the comma, if it's in there, before the cast!

Code: Select all

<?

function readCSV ( $filePath )
{
	$array = array ();
	$start = 1;
	$handle = fopen ( $filePath, 'r' );
	while ( ( $data = fgetcsv ( $handle, 1000, ',' ) ) !== false )
	{
		$data = str_replace ( ',', '', $data );

	$array[$start++] = array ( 
		'day' => $data[0], 
		'date' => intval ( $data[1] ), 
		'pageLoads' => intval ( $data[2] ), 
		'unique' => intval ( $data[3] ), 
		'firstTime' => intval ( $data[4] ), 
		'returning' => intval ( $data[5] )
	);
   }

   fclose ( $handle );
   return $array;
}

$file = './file.txt';

$out = readCSV ( $file );

print_r ( $out );

?>

pif!