Blog
14FebPHP: Extra array functions
Posted on February 14, 2011 in PHP byDuring my recent PHP/TYPO3 adventures I had to come up with some extra array functionality to grab my desired data from an array. Shockingly I couldn't find any official PHP way to do this. So I created my own.
Getting the average value of an array 
- function array_average($array) {
- $total = count($array);
- $sum = array_sum($array);
- return round($sum / $total, 0);
- }
function array_average($array) {
$total = count($array);
$sum = array_sum($array);
return round($sum / $total, 0);
}
Getting the minimum value of an array 
- function array_minimum($array) {
- sort($array);
- $minimum = array_shift($array);
- return $minimum;
- }
function array_minimum($array) {
sort($array);
$minimum = array_shift($array);
return $minimum;
}
Getting the maximum value of an array 
function array_maximum($array) {
sort($array);
$maximum = array_pop($array);
return $maximum;
}
Like I said, I couldn't find any quicker way to do this. Of course if you have any other suggestions, they are more than welcome.



at 10:11
Yes there are functions that do this
http://php.net/manual/en/function.max.php
http://php.net/manual/en/function.min.php
As for your average function, it doesn't seem there is a standard PHP function for this.
at 10:58
RE: Yes there are functions that do this
Hi Alex,
Thanks for the contribution. The min() and max() versions are indeed more versatile. Unfortunately one does not always know where to look. As I looked through the array functions it's no mystery why I didn't find them, as they are listed under the math functions :-)
Cheers,
Sebastiaan
at 13:36
moving average
regarding the average function, it _might_ be faster to calculate the average as the array is being built, rather than calculating it at the end, thus elimating the need for the function entirely.