Logo
Mean, Median, Mode, Range Of An Array
Online Now: 0
Snippet Code
This function will take an array, and either display the: mean, median, mode, or range of the array.

Introduction


The function mmmr (Mean, Median, Mode, Range) will calculate the Mean, Median, Mode, or Range of an array.
The function automatically defaults to Mean (average).

int mmmr( array $array [, str $output] )

functions.php


<?php
function mmmr($array$output 'mean'){
    if(!
is_array($array)){
        return 
FALSE;
    }else{
        switch(
$output){
            case 
'mean':
                
$count count($array);
                
$sum array_sum($array);
                
$total $sum $count;
            break;
            case 
'median':
                
rsort($array);
                
$middle round(count($array) / 2);
                
$total $array[$middle-1];
            break;
            case 
'mode':
                
$v array_count_values($array);
                
arsort($v);
                foreach(
$v as $k => $v){$total $k; break;}
            break;
            case 
'range':
                
sort($array);
                
$sml $array[0];
                
rsort($array);
                
$lrg $array[0];
                
$total $lrg $sml;
            break;
        }
        return 
$total;
    }
}

$arr = array(12,33,23,4,20,124,4,2);

// Mean = The average of all the numbers
echo 'Mean: '.mmmr($arr).'<br>';
echo 
'Mean: '.mmmr($arr'mean').'<br>';

// Median = The middle value after the numbers are sorted smallest to largest
echo 'Median: '.mmmr($arr'median').'<br>';

// Mode = The number that is in the array the most times
echo 'Mode: '.mmmr($arr'mode').'<br>';

// Range = The difference between the highest number and the lowest number
echo 'Range: '.mmmr($arr'range');
?> 
 
Snippet Comments

Add Your Comment

types of doctor
2011-06-25 18:31:41
lol so difficult. Would you like to post how we can use this script? Thank you.
ponte vedra dentist
2011-06-21 18:38:20
The script is so amazing to use. You will not be disappointed at all with it. The ideas are so good.
confidence
2011-06-01 01:03:23
The organizations urging members of congress to oppose efforts to weaken stability protections.Thanks for sharing the informative post. Regards. Roy - keyword
kamela
2011-05-31 21:49:51
We offer you Custom Presentation Folders that is not only good to look at but are also much significant to the boost that it can offer to both organization’s status and also its representation. In fact when it comes to the custom folder printing companies, they offer Custom Presentation Folders in several designs and styles as they recognize it significance to a business at wholesale price.
best fire equipment
2011-05-20 21:21:56
I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that:..
Buy Dissertation
2011-05-16 14:39:05
Das Buch "Mac OS X - Leopard" richtet sich an all jene, die Erfahrung mit anderen Betriebssystemen (wie Windows) haben und neu in die Leopard-Welt eintauchen wollen.
php scripts
2010-09-14 00:28:01
lol so difficult. Would you like to post how we can use this script? Thank you.

Add Your Comment

 
Snippet Tools
Rate this Snippet:

Rate the difficulty level:

Request Snippet Update


Suggested Difficulty Level: Novice
Current Score: 2.92
Total votes: 166
Total Views: 9343

Other top snippets by admin:

1. JPEG to ASCII Converter
2. Add (th, st, nd, rd, th) to the end of a number
3. Dynamic Page Content From Links
4. AJAX Quickie
5. Simple Image CAPTCHA

Search
Search for:







User Panel

User name:

Password:

Register And Post Your Own Snippets

New Snippets

1.  Shorten text and (4 of 1)

2.  file-based simple guest (0 of 0)

3.  adding an entry (0 of 0)

4.  integer to currency (1.33 of 3)

5.  Aggregate IP addresses (0 of 0)

6.  file handling (0 of 0)

7.  testing system with (0 of 0)

8.  Make an php (1 of 1)

9.  allow download in (5 of 1)

10.  redirect in 3 (0 of 0)

11.  live clock with (0 of 0)

12.  visit counter (3.5 of 2)

13.  contact form with (4 of 1)

14.  show a specific (0 of 0)

15.  The & sign (0 of 0)

16.  static variable in (0 of 0)

17.  multiplication table with (0 of 0)

18.  function with multiplicationt (0 of 0)

19.  php generated menu (0 of 0)

20.  drawing a table (0 of 0)

21.  embedded array (one (0 of 0)

22.  checks the week (0 of 0)

23.  put links on (4.5 of 2)

24.  Directory Image Gallery (0 of 0)

25.  TOS (4 of 1)

Home | Forum | Free PHP Web Hosting | Contact | Terms & Conditions |  
Donate
PHPSnips.com - ©2012 Amethyst Creative