Jump to content

CSV to array calculations


wesleyberry

Recommended Posts

Hi,

 

I have been set a task to use 300000 values from a csv to find the total amount of the values and the averages of the values (mode, mean and median).

 

The csv is set into 2 columns, number and amount with 300000 records for each. I am unsure as to how to use only the values from one of the columns in order to get the results i need. Below is the code I have so far;

 

$filepath = "testdata.csv";
$file = fopen($filepath, "r") or die("Error opening file");
$i = 0;

while(($line = fgetcsv($file)) !== FALSE) {
    if($i == 0) {
        $c = 0;
        foreach($line as $col) {
            $cols[$c] = $col;
            $c++;
        }
    } else if($i > 0) {
        $c = 0;
        foreach($line as $col) {
            $data[$i][$cols[$c]] = $col;
            $c++;
        }
    }
    $i++;
}

//this prints the whole of the data still
while (($cols[1])) print_r($data);

//this still prints all the data!!
while ($data) print_r($cols[1].$data);

 

Am I going about this wrong by using an associative array?

Link to comment
https://forums.phpfreaks.com/topic/248684-csv-to-array-calculations/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.