Zero-Affect Posted May 5, 2010 Share Posted May 5, 2010 I was watching Cube and came up with a idea for a function, i have no idea if it already exists though but it may come in helpful. function avgval($avg_vals) { if ( is_array($avg_vals) && count($avg_vals) > 1 ) { $return_vals = ( array_sum($avg_vals) / count($avg_vals) ); } elseif ( is_array($avg_vals) && count($avg_vals) == 1 ) { $return_vals = current($avg_vals); } else { $return_vals = FALSE; } return $return_vals; } echo avgval(array(6,11,7)); // outputs 8 echo avgval(array(6)); // outputs 6 Quote Link to comment Share on other sites More sharing options...
Zero-Affect Posted May 6, 2010 Author Share Posted May 6, 2010 Also thought i'd make up another version which works on string rather than array (im sure the two could be collided). function avgvals($avg_vals,$avg_delimiter=',') { if ( (is_string($avg_vals) && strlen($avg_vals) > 2) && (is_string($avg_delimiter) && !empty($avg_delimiter)) ) { $average_vals = explode($avg_delimiter, $avg_vals); $return_vals = ( array_sum($average_vals) / count($average_vals) ); } elseif ( (is_string($avg_vals) && strlen($avg_vals) <= 2) && (is_string($avg_delimiter) && !empty($avg_delimiter)) ) { $return_vals = $avg_vals; } else { $return_vals = FALSE; } return $return_vals; } echo avgvals('6,11,7'); // outputs 8 echo avgvals('6-11-7', '-'); // outputs 8 echo avgvals('6'); // outputs 6 Quote Link to comment Share on other sites More sharing options...
Djkanna Posted May 6, 2010 Share Posted May 6, 2010 This could actually be quite handy. Nice work Zero! Quote Link to comment Share on other sites More sharing options...
Zero-Affect Posted May 6, 2010 Author Share Posted May 6, 2010 I submitted them to php.net in notes so im sure people will use them. Quote Link to comment Share on other sites More sharing options...
Spudinski Posted May 9, 2010 Share Posted May 9, 2010 PHP isn't new, neither is mathematics - everything you will ever think of has been done before. I find these two function to be more productive: http://www.mdj.us/web-development/php-programming/calculating-the-median-average-values-of-an-array-with-php/ Edit: Now that I think of it, why the hell doesn't PHP have an average or median function? When I learned Delphi my teacher drilled my with those functions(sqrt, cos and sin was more severe, but still). Quote Link to comment Share on other sites More sharing options...
Zero-Affect Posted May 10, 2010 Author Share Posted May 10, 2010 function calculate_average($arr) { $count = count($arr); //total numbers in array foreach ($arr as $value) { $total = $total + $value; // total value of array numbers } $average = ($total/$count); // get average value return $average; } compared to mine function avgval($avg_vals) { if ( is_array($avg_vals) && count($avg_vals) > 1 ) { $return_vals = ( array_sum($avg_vals) / count($avg_vals) ); } elseif ( is_array($avg_vals) && count($avg_vals) == 1 ) { $return_vals = current($avg_vals); } else { $return_vals = FALSE; } return $return_vals; } I'd say mine is better and more efficient really... Yeah i published my notes on php.net hope they maybe adopt the functions in some way that would be nice. Quote Link to comment Share on other sites More sharing options...
rulerofzu Posted July 18, 2010 Share Posted July 18, 2010 Just so happens that earlier today I needed such a function. So thanks for this. Quote Link to comment Share on other sites More sharing options...
Zero-Affect Posted July 18, 2010 Author Share Posted July 18, 2010 Your welcome, glad i could help. 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.