php - Incorrect values functions -


i have problem script, script read file (csv) , stores last value of each line in array, calculate maximun, minimun , average of values in array, big files script give me incorrect value maximun , minimun, here can download script , 2 files, file 8.csv calculations correct file 1.csv values wrong, difference see file 1.csv larger other.

this code:

<?php $variable2=file("1.csv"); $i=0; foreach($variable2 $var){     if($i==0){         $i++;     }else{         $datos=explode(",",$var);         $valor=$datos[count($datos)-1];         if($valor!= -3000){             $todos[$i-1]=$valor;             $i++;         }     } }  $promedio=array_sum($todos) / count($todos); $maximo=max($todos); $minimo=min($todos); echo "maximo = ".$maximo." minimo = ".$minimo." promedio = ".$promedio;  ?> 

and part of file script read:

objectid,pointid,grid_code,potrero_id,mod13q1.a2 7300.0,7300.0,1.0,1,6431 7498.0,7498.0,1.0,1,6684 7499.0,7499.0,1.0,1,6431 7500.0,7500.0,1.0,1,6431 7501.0,7501.0,1.0,1,6431 7502.0,7502.0,1.0,1,6431 7503.0,7503.0,1.0,1,6431 7504.0,7504.0,1.0,1,6304 7697.0,7697.0,1.0,1,6734 7698.0,7698.0,1.0,1,6734 7699.0,7699.0,1.0,1,6127 7700.0,7700.0,1.0,1,6127  expected values:  maximun: 9307 minimun: -650 average: 6555,211347  output values:  maximun: 999 minimun: -104 average: 6555,3296310272 

you can give try on code

<?php if (($handle = fopen("1.csv", "r")) !== false) {     $todos = array();     $datos = fgetcsv($handle, 1000, ",");     while (($datos = fgetcsv($handle, 1000, ",")) !== false) {         $valor=$datos[count($datos)-1];         if($valor != -3000){             $todos[]=$valor;         }     }     $promedio=array_sum($todos) / count($todos);     $maximo=max($todos);     $minimo=min($todos);     echo "maximo = ".$maximo." minimo = ".$minimo." promedio = ".$promedio;     fclose($handle); } ?> 

here made use of fgetcsv(); reads file handle , returns array of entire row csv can iterate over, initialized $dotos = array(); once within values entered if condition becomes true.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -