sandy1028 Posted July 25, 2007 Share Posted July 25, 2007 Hi, How to find the maximum value from the file. $lines = file('data.txt'); foreach ($lines as $line) { $text_line = explode(":",$line); In $text_line[2] i should find the maximum value and average value.... using max($text_line[2]) I am getting error Quote Link to comment Share on other sites More sharing options...
shivani.shm Posted July 25, 2007 Share Posted July 25, 2007 if your values are in a array use asort(your array in here) and sort the array and the last one will be your max value just use print_r the array and chk the results... Quote Link to comment Share on other sites More sharing options...
tibberous Posted July 25, 2007 Share Posted July 25, 2007 That is because $text_line[2] only has a single value. It's like saying max(7) and expecting to get the largest number. What you want to do is keep storing $text_line[2] into $arr[count($arr)], then at the end of the for each do a max $arr. Quote Link to comment Share on other sites More sharing options...
dg Posted July 25, 2007 Share Posted July 25, 2007 u should give max($text_line) ......... $text_line is a array if i have understood properly Quote Link to comment Share on other sites More sharing options...
sandy1028 Posted July 25, 2007 Author Share Posted July 25, 2007 hi, The $text_line[2] is not an array..... The result in $text_line[2] is 23 45 23 634 235 35345 453 In this line I need the largest and the average number Quote Link to comment Share on other sites More sharing options...
dg Posted July 25, 2007 Share Posted July 25, 2007 ok ... than explode $text_line[2] on space ... and than apply the max function..... Quote Link to comment Share on other sites More sharing options...
dg Posted July 25, 2007 Share Posted July 25, 2007 and for average u can use array_sum divided by count Quote Link to comment Share on other sites More sharing options...
sandy1028 Posted July 25, 2007 Author Share Posted July 25, 2007 How to find the large number when read from the text file. In the file the $text_line[2] is the numerical values. How to find the largest number When I print the $text_line[2] output is 32 23 23 34 23 56 23 222 123 67 To sort as array and find the maximum value this is not the array Any help regarding this to find the large number Quote Link to comment Share on other sites More sharing options...
dg Posted July 25, 2007 Share Posted July 25, 2007 as what u said earlier ... it seems $text_line[2] is a string of 32 23 23 34 23 56 23 222 123 67 ........ is it .... so make it a array like $aTest = explode(' ', $text_line[2]); // to get max value max($aTest); // to get average ... array_sum($aTest)/count($aTest); hope this is wht u want ...... Quote Link to comment Share on other sites More sharing options...
sandy1028 Posted July 25, 2007 Author Share Posted July 25, 2007 hi, Ok thanks..... Convert string values to integer because 122 is greater than 99. 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.