# Median() Method | How It is Different From avg() Method In Laravel Collection

• 06-07-2022
• Laravel 9

In this post, we will learn the difference between average and median laravel collections. the median will return the middle value of the collection while the average method will take some of the whole collection and divide it into numbers for collection item.

Here in this post, I have take little understanding between median() laravel collection and avg() laravel collection.

So let's see how it looks different with some examples.

Example 1 | median() v/s avg()

##### median()

You can see in this example median() will take the middle of the array value from the collection.

```\$data = [
['price' => 50],
['price' => 60], // this will be median
['price' => 70],
];

return collect(\$data)->median('price');

// OUTPUT

// 60
```
##### avg()

Here average will sum of the total collection of the price and device number of the count with the total collection.

```\$data = [
['price' => 50],
['price' => 60],
['price' => 70],
];

return collect(\$data)->avg('price');

// OUTPUT

// ((50 + 60 + 70) / 3) = 60

// 60
```

Example 2 | median() v/s avg()

##### median()

In this example you can see we have four collections, So median will take two of the middle collection and sum it and after divide with two.

```\$data = [
['price' => 50],
['price' => 60], // this
['price' => 70], // this
['price' => 70],
];

return collect(\$data)->median('price');

// OUTPUT

// ((60 + 70) / 2) = 65

// 65
```
##### avg()

Here avg() will take the sum of the whole collection and then divide it with the number of the collection count and will return the average.

```\$data = [
['price' => 50],
['price' => 60],
['price' => 70],
['price' => 70],
];

return collect(\$data)->avg('price');

// OUTPUT

// ((50 + 60 + 70 + 70) / 4) = 50

// 50
```

Example 3 | median() v/s avg()

##### median()

In this example in the collection we have five-item of array prices, So here median will take the middle value from the collection and will return it.

```\$data = [
['price' => 50],
['price' => 60],
['price' => 70], // this
['price' => 80],
['price' => 90],
];

return collect(\$data)->median('price');

// OUTPUT

// 70
```
##### avg()

Same example avg() will sum to the total collection of price and device total number of price count in the collection and return.

```\$data = [
['price' => 50],
['price' => 60],
['price' => 70],
['price' => 80],
['price' => 90],
];

return collect(\$data)->avg('price');

// OUTPUT

//((50 + 60 + 70 + 80 + 90 ) / 5) = 70

// 70
```

Example 4 | median() v/s avg()

##### median()

In the further example you can see in this example we have six different price collections, median will take the middle of two, and the sum after divided with two.

```\$data = [
['price' => 50],
['price' => 60],
['price' => 70], // this
['price' => 80], // this
['price' => 90],
['price' => 100],
];

return collect(\$data)->median('price');

// OUTPUT

// ((80 + 70) / 2) = 75

// 75
```
##### avg()

While average will sum of total price collection and device to total number to collection price count return return the value.

```\$data = [
['price' => 50],
['price' => 60],
['price' => 70],
['price' => 80],
['price' => 90],
['price' => 100],
];

return collect(\$data)->avg('price');

// OUTPUT

// ((50 + 60 + 70 + 80 + 90 + 100) / 6) = 75

// 75
```

So you can understand what is different between avg() and median() now in laravel collection.

We always thanks to you for reading our blogs. Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us Haresh Chauhan

(Co-Founder)