sandy1028 Posted July 27, 2007 Share Posted July 27, 2007 Hi, I want to find the maximum and minimum, average value from the text file <? $lines = file('./data.txt'); foreach ($lines as $line) { $text_line = explode(":" , $line); $text_line1 = explode(" " , $text_line[2]); array_sum($text_line1); } ?> This method I am not able to get the array sum..... How to get the numerical value of numbers from strings When I explode the line it is in string format.... I should find the array_sum and max() which I values are in numeric not string Quote Link to comment Share on other sites More sharing options...
sandy1028 Posted July 27, 2007 Author Share Posted July 27, 2007 Hi, I am facing problem here not able to fetch the exact values of averag and sum It is fetching the last value I should pass the value of avg and maximum in to array $graphValues=array($avg,$max); <?php $lines = file('./phonedb.txt'); $count= count($lines); foreach ($lines as $line) { $arrLineValues = explode(":" , $line); $arrField2Values = explode(" " , $arrLineValues[2]); $max = max($arrField2Values); $sum = array_sum($arrField2Values); $avg = $sum/$count; } ?> Quote Link to comment Share on other sites More sharing options...
hvle Posted July 27, 2007 Share Posted July 27, 2007 try this <?php $lines = file('./phonedb.txt'); $count= count($lines); //added $allNumbers = array(); foreach ($lines as $line) { $arrLineValues = explode(":" , $line); $arrField2Values = explode(" " , $arrLineValues[2]); $allNumbers = array_merge($allNumbers, $arrField2Values); } $max = max($allNumbers); $sum = array_sum($allNumbers); $avg = $sum/count($allNumbers); ?> Quote Link to comment Share on other sites More sharing options...
sandy1028 Posted July 27, 2007 Author Share Posted July 27, 2007 Hey thanks it is working Quote Link to comment 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.