put user inputted numbers into ascending order

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You will have to use the array key (maybe something like array_keys()).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ahem.. arsort().
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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. ;)
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post 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?
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post 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?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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;
}
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post 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 ;)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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. :lol:
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

i'm so confused dude :?:
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
Post Reply