Jump to content

PHP incorrect output values functions max,min


killemall
Go to solution Solved by killemall,

Recommended Posts

i have a problem with a script, the script read a file (csv) and stores the last value of each line in an array, then i calculate the maximun,minimun and average of the values in the array, but with big files the script give me an incorrect value for the maximun,minimun and average, with the file 8.csv the calculations are correct but with the file 1.csv the values are wrong, the only difference i see is that the file 1.csv is much larger than the other. Thanks for your help!!!

 

This is the code:

<?php
$variable2=file("1.csv");
$i=0;
foreach($variable2 as $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 this is a part of the file 1.csv (the full file have more than 60.000 lines)

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

 

If you want you can download the code and input files here:

 

https://www.dropbox.com/s/y4vpjm6086rj3s3/script.zip

 

Thanks.

 

Link to comment
Share on other sites

the problem is because the new-line character on the end of each line is being stored in the $todos array. this is causing the min/max to treat the values as strings. use the trim function to remove the new-line characters -

$valor=trim($datos[count($datos)-1]);

you could also use the FILE_IGNORE_NEW_LINES flag in the file() statement.

Edited by mac_gyver
Link to comment
Share on other sites

  • Solution

the problem is because the new-line character on the end of each line is being stored in the $todos array. this is causing the min/max to treat the values as strings. use the trim function to remove the new-line characters -

$valor=trim($datos[count($datos)-1]);

you could also use the FILE_IGNORE_NEW_LINES flag in the file() statement.

 

Thanks, that was the problem :)

Link to comment
Share on other sites

Hi I have similar situation bur rather I want to find the maximum value for each each column. Here's  my code. How so I go about it?

	$handle=file('ftp://test:jumoke@localhost/test.csv');
	\\
	foreach ($handle as $row) {
		
	 list($col[1][], $col[2][], $col[3][], $col[4][], $col[4][], $col[5][], $col[6][],
	$col[7][], $col[8][], $col[9][], $col[10][], $col[11][]) = explode(',', $row);
		//var_dump(explode(',', $row));
	Var_dump($col[1]); 
	}
Link to comment
Share on other sites

a) don't hijack other peoples threads, use your existing thread

 

b) in your own thread for this problem, you didn't state you want to find the maximum value for each column. you stated you wanted to find the maximum (the title), the minimum (in the thread), for a particular column. you need to clearly state what you are trying to find.

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.