PHP Program To Find Mean and Median of an Unsorted Array Last Updated : 13 May, 2024 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 Comment More infoAdvertise with us Next Article PHP Program To Find Mean and Median of an Unsorted Array B blalverma92 Follow Improve Article Tags : PHP Similar Reads PHP program to find the maximum and the minimum in array Finding the maximum and minimum in an array involves determining the largest and smallest values within a given set of numbers. This task is crucial for analyzing data, identifying trends, or filtering outliers. Various methods, from simple traversal to optimized algorithms, can achieve this.Example 7 min read PHP program to find the Standard Deviation of an array Given an array of elements. We need to find the Standard Deviation of the elements of the array in PHP. Examples: Input : array(2, 3, 5, 6, 7)Output : 1.5620499351813Input : array(1, 2, 3, 4, 5)Output : 1 The following problem can be solved using the PHP inbuilt functions. The inbuilt functions used 2 min read PHP Program for Median of two sorted arrays of same size Write a PHP program for a given 2 sorted arrays A and B of size n each. the task is to find the median of the array obtained by merging the above 2 arrays(i.e. array of length 2n). The complexity should be O(log(n)). Examples: Input: ar1[] = {1, 12, 15, 26, 38} ar2[] = {2, 13, 17, 30, 45}Output: 16E 6 min read Mean, Median and Mode| Comparison, Relationship and Calculation A single value used to symbolise a whole set of data is called the Measure of Central Tendency. In comparison to other values, it is a typical value to which the majority of observations are closer. Average and Measure of Location are other names for the Measure of Central Tendency. In statistical a 7 min read Examples of How to Find Median of Data Median is a key measure of central tendency in statistics, representing the middle value of a data set when arranged in ascending or descending order. Unlike the mean, the median is not affected by extreme values, making it a reliable measure for skewed distributions.In this article, we will explore 8 min read Like