avg() public method

Get the average value of a given key.
public avg ( callable | string | null $callback = null ) : mixed
$callback callable | string | null
return mixed
Example #1
0
 public function testGettingAvgItemsFromCollection()
 {
     $c = new Collection([(object) ['foo' => 10], (object) ['foo' => 20]]);
     $this->assertEquals(15, $c->avg('foo'));
     $c = new Collection([['foo' => 10], ['foo' => 20]]);
     $this->assertEquals(15, $c->avg('foo'));
     $c = new Collection([1, 2, 3, 4, 5]);
     $this->assertEquals(3, $c->avg());
     $c = new Collection();
     $this->assertNull($c->avg());
 }
 public function avg($key = null)
 {
     return round(parent::avg($key), 2);
 }