2017 © Pedro Peláez
 

library php-array-avg-callable

Calculates average of elements of array given by callable function

image

outcloud/php-array-avg-callable

Calculates average of elements of array given by callable function

  • Monday, November 13, 2017
  • by Macintoshx
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

php_array_avg_callable

Calculates average value from array of objects/numbets via callable function, (*1)

Example usage:

Let's assume we have any object of any class with some numeric value inside. We have an array of that objects, and we have to calculate a average of those numeric values., (*2)

Let's this be our class, (*3)

class Num{
    private $value;

    public function __construct(int $number){
        $this->value = $number;
    }
    public function getValue(){
        return $this->value;
    }
}

we have array of items of 'Num' class, (*4)

$items = [
    new Num(1),
    new Num(2),
    new Num(3),
];

Let's use array_avg_callable to calculate avg of that numbers, (*5)

array_avg_callable($items, function($item){
    return $item->getValue();
});

Result will of course be: 2, (*6)

Return Values

Returns the avg of values as an integer or float; an exception will be thrown if array is empty, (*7)

Installation

via Composer:, (*8)

composer require outcloud/php-array-avg-callable

function will be registered in global namespace., (*9)

The Versions

13/11 2017

dev-master

9999999-dev

Calculates average of elements of array given by callable function

  Sources   Download

MIT

The Requires

 

by Grzegorz Lieske

13/11 2017

1.0.0

1.0.0.0

Calculates average of elements of array given by callable function

  Sources   Download

MIT

The Requires

 

by Grzegorz Lieske