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 Link to comment https://forums.phpfreaks.com/topic/61673-to-find-the-number/ 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... Link to comment https://forums.phpfreaks.com/topic/61673-to-find-the-number/#findComment-306989 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. Link to comment https://forums.phpfreaks.com/topic/61673-to-find-the-number/#findComment-306990 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 Link to comment https://forums.phpfreaks.com/topic/61673-to-find-the-number/#findComment-306991 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 Link to comment https://forums.phpfreaks.com/topic/61673-to-find-the-number/#findComment-306994 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..... Link to comment https://forums.phpfreaks.com/topic/61673-to-find-the-number/#findComment-307008 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 Link to comment https://forums.phpfreaks.com/topic/61673-to-find-the-number/#findComment-307009 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 Link to comment https://forums.phpfreaks.com/topic/61673-to-find-the-number/#findComment-307011 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 ...... Link to comment https://forums.phpfreaks.com/topic/61673-to-find-the-number/#findComment-307013 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. Link to comment https://forums.phpfreaks.com/topic/61673-to-find-the-number/#findComment-307020 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.