Calculate Average of an Array of Numbers in PHP



The average or mean of an array is a basic mathematical operation in programming. The average is used in analytics, finance, and reporting. One of the real-life applications is a website that calculates the average rating of a product or the average score of a test. In this article, we are going to learn how we can calculate the average of an array of numbers in PHP using different approaches.

What is Average?

The average is obtained by calculating the sum of all elements in an array and then dividing the sum by the total number of elements in the array.

The formula for calculating the Average of an array of Numbers = (Sum of all elements in given Array) / (Total Number of Elements in given Array)

How to Calculate the Average of an Array?

The average of an array is calculated by first adding up all the elements in the array. Then, divide the sum by the total number of elements in the array.

Example 1

Input

Array = [1, 2, 3, 4, 5]

Output

Average = 3

Explanation

(1 + 2 + 3 + 4 + 5)/3 = 15 / 5 = 3

Example 2

Input

Array = [1, 3, 5, 7, 9]

Output

Average = 5

Explanation

(1 + 3 + 5 + 7 + 9) / 5 = 25 / 5 = 5

Example 3

Input

Array = [10, 20, 30, 40, 50, 60]

Output

Average = 20

Explanation

(10 + 20 + 30 + 40 + 50 + 60) / 6 = 210 / 6 = 35

Below are different approaches to calculate the average of an array in PHP

Using Built-in Functions

PHP has built-in functions that allow direct calculation of averages. array_sum() function is used to compute the sum of elements of the array. count() function is used to count the number of elements present in the array.

Steps for Implementation

  • Define an array.
  • Use array_sum() to calculate the total sum of the elements.
  • Use count() to find the number of elements.
  • Calculate the average by dividing the sum by the count.
  • Output the result.

Implementation Code

<?php
// Define an array
$numbers = [1, 2, 3, 4, 5];

// Calculate the sum of all elements in the array
$sum = array_sum($numbers);

// find the total count of elements in the array
$count = count($numbers);

// Calculate the average
$average = $sum / $count;

// Output
echo "The average of the array is: $average";
?>

Output

The average of the array is: 3

Time Complexity - O(n)

Space Complexity O(1)

Using Loop

In this approach, we iterate through the array using the loop find the sum of all the elements, and then calculate the average.

Steps for Implementation

  • Define an array.
  • Initialize a variable to store the sum.
  • Use a loop to traverse the array and add each element to the sum variable.
  • Calculate the average by dividing the sum by the count.
  • Output the result.

Implementation Code

<?php
// Define an array
$numbers = [1, 2, 3, 4, 5];

// Initialize a variable to store the sum
$sum = 0;

// total count of elements in the array
$count = count($numbers);

// Loop through each element in the array to calculate the sum
foreach ($numbers as $num) {
$sum += $num; // Add each element to the sum
}

// Calculate the average
$average = $sum / $count;

// output
echo "The average of the array is: $average";
?>

Output

The average of the array is: 3

Time Complexity - O(n)

Space Complexity - O(1)

Using Array Reduce Function

In this approach, we use PHP's array_reduce() function to calculate the sum of array elements and then divide by the count of elements.

Steps for Implementation

  • Define an array of numbers.
  • Use array_reduce() to find the sum of the array elements.
  • Divide the sum by the count of elements to calculate the average.
  • Output the result.

Implementation Code

<?php
// Define an array
$numbers = [2, 4, 6, 8, 10];

// Calculate the average using array_reduce()
$sum = array_reduce($numbers, function($carry, $item) {
return $carry + $item;
}, 0);

// Calculate the average by dividing the sum by the total number of elements
$average = $sum / count($numbers);

// Output
echo "The average of the array is: $average";
?>

Output

The average of the array is: 6

Time Complexity - O(n)

Space Complexity - O(1)

Real-life Applications of Calculating the Average of an Array

  • It is used in statistical analysis to find mean values (average) of given datasets.
  • It is used in grading systems to calculate the average marks of students in schools and colleges.
  • It helps in financial applications to calculate the average expenses over a given period of time.
  • It is used in sports analytics to determine the average performance of players.
Updated on: 2025-03-28T16:24:33+05:30

222 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements