Jump to content

finding the maximum number of a column from a csv file


jadeg

Recommended Posts

I have a csv file and I am trying to get the minimum value for a particular column. However I am not getting an exact value even after exploding the values. After I explode and I do a var_dump on it, it produces a different array for each different row; so each value in the column is the first element of a different array. How do I merge this different arrays into one array in order to find the minimum value in the column, or is there any other way to go about it? Below is my code. Any help will be appreciated.

 

 

My code is below and a link to the excel file 

https://www.dropbox.com/s/n7pyv96qv1yv8yc/Example%20SG%20Data.xlsxwhile (($data = fgetcsv($handle, 0, ",")) !== false) {


$table2Add =false;
if($row >=$x && $row <=$y) {
$table2Add=true; }


if($row == 1) {
$table2 .= '<thead><tr>';
$table2 .= '<th></th>';
$table2 .= '<th>Min</th>';
$table2 .= '<th>Max</th>';
$table2 .= '<th>Average</th>';
$table2 .= '</tr></thead><tbody>';


}
else {

if($table2Add) $table2 .= '<tr><th>Voltage</th>';
for($c = 0; $c <= 10; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];

if($c==1) {
var_dump((explode('STRING',$data[$c])));
//$ar=explode('STRING', $data[$c]);

//var_dump(min($ar));

if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==2) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==3) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
}

if($table2Add) $table2 .= '</tr>';



if($table2Add) $table2 .= '<tr><th>Current</th>';
for($c = 0; $c <= 10; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
if($c==4) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==5) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==6) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
}

if($table2Add) $table2 .= '</tr>';


if($table2Add) $table2 .= '<tr><th>Power Factor</th>';
for($c = 0; $c <= 10; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
if($c==7) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==8) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==9) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
}

if($table2Add) $table2 .= '</tr>';



}
$row++;
}

It would be easier to check your code if you use the "code" button and indent your code.

Also, you should check out the str_getcsv() method: http://www.php.net/manual/en/function.str-getcsv.php

I never used it but it seems done for what you want to do.

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.