Illuminate\Support\Collection::avg PHP Method

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
    public function avg($callback = null)
    {
        if ($count = $this->count()) {
            return $this->sum($callback) / $count;
        }
    }

Usage 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());
 }
All Usage Examples Of Illuminate\Support\Collection::avg