Re: Re: get the median from an array of integers
| From: | Erik Price | Date: | Fri, 01 Mar 2002 20:23:12 +0000 |
| Subject: | Re: Re: get the median from an array of integers | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-86837@lists.php.net to get a copy of this message | ||
On Friday, March 1, 2002, at 02:28 PM, Jason Wong wrote:
On Saturday 02 March 2002 02:03, Daniel Grace wrote:I told you guys I was bad at math! I don't even remember the terms... thanks to everyone who replied to this thread with advice, it looks like I can do: - grab all values from the desired inputs and slap them into an array - run the array against array_count_values() to get the number of each input value in that array (in the form of a new array) - rsort the resulting new array (of 'value => occurrences' form) to put the highest-numbered array at the beginning of the array - the key of the first element in this rsorted array is the value I am looking for -- the most-chosen value from the initial user inputs - array_flip the array inside the first element of the rsorted array to make the key into the value and vice versa - my desired value is now the value of the first element in the array It seems like a lot of work, but sounds logical. The end result is the most-chosen value from the user inputs becomes the value of the first element in the array returned by array_count_values() after that array has been rsorted and flipped. Crazy! Erik PS: can anyone see a flaw in this scheme? Can anyone even tell what I'm talking about? ---- Erik Price Web Developer Temp Media Lab, H.H. Brown pricee@hhbrown.comAs for the median value, there's a relatively easy way to do this in PHP: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();