Jump to content
MakeWebGames

avgval function


Zero-Affect

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...