PHP Program To Find Mean and Median of an Unsorted Array Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given an unsorted array, the task is to find the mean (average) and median of the array in PHP. we will see the approach and code example to solve this problem. ApproachTo find the mean of an array, we need to sum up all the elements in the array and then divide the sum by the total number of elements in the array. To find the median of an array, we first need to sort the array in ascending order. If the array has an odd number of elements, the median is the middle element. If the array has an even number of elements, the median is the average of the two middle elements. Example: This example shows the implementation of the above-explained approach. PHP <?php function findMean($arr) { $sum = array_sum($arr); $count = count($arr); return $sum / $count; } function findMedian($arr) { sort($arr); $count = count($arr); $middle = floor($count / 2); if ($count % 2 == 0) { $median = ($arr[$middle - 1] + $arr[$middle]) / 2; } else { $median = $arr[$middle]; } return $median; } // Driver Code $arr = [10, 15, 5, 15, 7, 18]; $mean = findMean($arr); echo "Mean of Unsorted Array: " . $mean; $mean = findMedian($arr); echo "\nMedian of Unsorted Array: " . $mean; ?> OutputMean of Unsorted Array: 11.666666666667 Median of Unsorted Array: 12.5 Create Quiz Comment B blalverma92 Follow 0 Improve B blalverma92 Follow 0 Improve Article Tags : PHP Explore BasicsPHP Syntax4 min readPHP Variables5 min readPHP | Functions6 min readPHP Loops4 min readArrayPHP Arrays5 min readPHP Associative Arrays4 min readMultidimensional arrays in PHP5 min readSorting Arrays in PHP4 min readOOPs & InterfacesPHP Classes2 min readPHP | Constructors and Destructors5 min readPHP Access Modifiers4 min readMultiple Inheritance in PHP4 min readMySQL DatabasePHP | MySQL Database Introduction4 min readPHP Database connection2 min readPHP | MySQL ( Creating Database )3 min readPHP | MySQL ( Creating Table )3 min readPHP AdvancePHP Superglobals6 min readPHP | Regular Expressions12 min readPHP Form Handling4 min readPHP File Handling4 min readPHP | Uploading File3 min readPHP Cookies9 min readPHP | Sessions7 min read Like