killemall Posted March 10, 2014 Share Posted March 10, 2014 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.A27300.0,7300.0,1.0,1,64317498.0,7498.0,1.0,1,66847499.0,7499.0,1.0,1,64317500.0,7500.0,1.0,1,64317501.0,7501.0,1.0,1,64317502.0,7502.0,1.0,1,64317503.0,7503.0,1.0,1,64317504.0,7504.0,1.0,1,63047697.0,7697.0,1.0,1,67347698.0,7698.0,1.0,1,67347699.0,7699.0,1.0,1,61277700.0,7700.0,1.0,1,6127Expected values:Maximun: 9307Minimun: -650Average: 6555,211347Output values:Maximun: 999Minimun: -104Average: 6555,3296310272 If you want you can download the code and input files here: https://www.dropbox.com/s/y4vpjm6086rj3s3/script.zip Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/286850-php-incorrect-output-values-functions-maxmin/ Share on other sites More sharing options...
mac_gyver Posted March 10, 2014 Share Posted March 10, 2014 (edited) 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 March 10, 2014 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/286850-php-incorrect-output-values-functions-maxmin/#findComment-1472007 Share on other sites More sharing options...
Ch0cu3r Posted March 10, 2014 Share Posted March 10, 2014 you could also use the FILE_IGNORE_NEW_LINES flag in the file() statement. Or use fgetcsv Quote Link to comment https://forums.phpfreaks.com/topic/286850-php-incorrect-output-values-functions-maxmin/#findComment-1472008 Share on other sites More sharing options...
Solution killemall Posted March 10, 2014 Author Solution Share Posted March 10, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/286850-php-incorrect-output-values-functions-maxmin/#findComment-1472009 Share on other sites More sharing options...
jadeg Posted March 10, 2014 Share Posted March 10, 2014 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]); } Quote Link to comment https://forums.phpfreaks.com/topic/286850-php-incorrect-output-values-functions-maxmin/#findComment-1472063 Share on other sites More sharing options...
mac_gyver Posted March 10, 2014 Share Posted March 10, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/286850-php-incorrect-output-values-functions-maxmin/#findComment-1472065 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.