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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.