Re: Re: get the median from an array of integers

From: Date: Fri, 01 Mar 2002 19:28:01 +0000
Subject: Re: Re: get the median from an array of integers
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-86834@lists.php.net to get a copy of this message
On Saturday 02 March 2002 02:03, Daniel Grace wrote: > > The web page features ten listboxes, each of which contains the same > > options. Let's say that you can choose A, B, C, D, or E from each > > listbox. Submit the form, run some code to figure out which was > > most-frequently selected, and then display that value on the next page. > > > > That's all I really want to do, but I'm pretty bad at math -- I could > > enter all the values into an array and run some kind of averaging code > > on them, but the problem is that I don't want an "average", I need the > > "most-chosen" value. > > As for the median value, there's a relatively easy way to do this in PHP: > > 1. Sort the array (sort()) > 2. Count the number of elements in the array (count()) > 3. Take the element that is equal to half the number of the elements > (rounded to an int, which way you round doesn't matter) > (really_easy_code()) > > Might be off-by-1 but that's fixed easily enough. I think Erik has gotten his terms mixed. He wants the "most-chosen" value, which is the "mode" and not "median". To get the mode, use array_count_values(); -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk /* Grief can take care of itself; but to get the full value of a joy you must have somebody to divide it with. -- Mark Twain */

« previous php.general (#86834) next »