Page 3 of 3
Posted: Fri Jun 01, 2007 3:58 pm
by tail
this is what i got so far:
Code: Select all
$predata = explode(", ", $_POST['data']);
$data = array_count_values($predata);
rsort($data);
echo 'Mode: '.$data[0].'<br>';
but that outputs the number 2. i'm guessing its outputting the number of times the number shows up but not the actual number. is there a way to get the actual number instead of just how many times it shows up? sorry but i've never messed around with arrays before.
Posted: Fri Jun 01, 2007 4:11 pm
by RobertGonzalez
You will have to use the array key (maybe something like
array_keys()).
Posted: Fri Jun 01, 2007 4:15 pm
by feyd
Posted: Fri Jun 01, 2007 4:21 pm
by tail
this worked for me thanks a lot:
Code: Select all
$predata = explode(", ", $_POST['data']);
$data = array_count_values($predata);
echo 'Mode: '.min(array_keys(($data))).'<br>';
but what if the inputted numbers were like: 12, 12, 13, 14, 14
is there a way to echo them both? or what if there was no most common number? could you just make it echo the word none?
Posted: Fri Jun 01, 2007 4:24 pm
by Chris Corbyn
If you hadn't sorted and you wanted the smallest and largest values then min() and max() functions may be of interest.
Posted: Fri Jun 01, 2007 4:59 pm
by RobertGonzalez
You know, given the nature of this thread, you probably would have been better off reading
the PHP manual section on arrays. In fact, if you scroll down that page to
about this point, you'll see a Table of Contents with all the array functions there for your taking.

Posted: Fri Jun 01, 2007 5:06 pm
by tail
tail wrote:but what if the inputted numbers were like: 12, 12, 13, 14, 14
is there a way to echo them both? or what if there was no most common number? could you just make it echo the word none?
can you do this? if so, what functions should i look at?
Posted: Fri Jun 01, 2007 9:43 pm
by tail
ok so right now i'm using this to find the middle number:
Code: Select all
$predata = explode(", ", $_POST['data']);
sort($predata);
$num = count($predata);
$mid = $num / 2;
echo '<b>Median:</b> '.$predata[$mid].'<br>';
but if there is an even amount of numbers, i need it to divide the two middle ones. can that be done?
Posted: Sat Jun 02, 2007 5:04 am
by superdezign
Sure. Use modulus.
Code: Select all
$mid = $num/2;
if(($mid % 1) != 0) // Since you're dividing by two, this will only be 0 or .5
{
$first = $mid - .5;
$second = $mid + .5;
}
Posted: Sat Jun 02, 2007 5:41 pm
by tail
but i need to echo the results onto the page. so if there were two numbers in the middle, i need to divide those by 2 and then echo that result. but if there is only one in the middle, just echo that result. also, the integer in the middle could be a decimal so it won't always be 0 or .5

Posted: Sat Jun 02, 2007 7:35 pm
by superdezign
Array indexes are integers.
According to the code that you posted, $num was the amount of indexes, and $mid was the index for the median.
Looking at it, I think I got the logic backwards however. An odd number of results will give you an existing value as a median, and $mid % 1 will equal 0.5.
The example wasn't meant to be your solution, but an example as to how you should go about trying to solve it. Try writing down all of the values you have, then write down all of the values that you want, and then use logic to go from what you have to what you want. Pace around a lot... It helps.

Posted: Sat Jun 02, 2007 9:13 pm
by tail
i'm so confused dude

Posted: Sat Jun 02, 2007 9:35 pm
by superdezign
superdezign wrote:Try writing down all of the values you have, then write down all of the values that you want, and then use logic to go from what you have to what you want.
Make up a situation, find the solutions logically, then use whatever variables you have at your expense to replicate those results.