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

User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Jcart wrote:
3. is there a way to extract just the largest integer?

Code: Select all

rsort($data);
echo $data[0];
Or, if it's already sorted:

Code: Select all

echo $data[sizeof($data) - 1];
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

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

Post by tail »

is there a way to extract which number appears the most?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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

Post by tail »

i'm confused by this. how would this extract the most common number in the array?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

array_count_values() wrote:array_count_values() returns an array using the values of the input array as keys and their frequency in input as values.
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

i used this but it outputted "Array" instead of the most common integer

Code: Select all

$predata = explode(", ", $_POST['data']);
$data = array_count_values($predata);
echo 'Mode: '.$data.'';
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

tail wrote:i used this but it outputted "Array" instead of the most common integer
Yes... Did you read?
feyd wrote:
array_count_values() wrote:array_count_values() returns an array using the values of the input array as keys and their frequency in input as values.

That means that $data[$value] = $occurences.

We don't have magic functions in PHP, but we do have enough functions to get done what needs to be done.
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

i still dont understand what the function is saying that it does. what does it mean when it says their frequency in input as values and input array as keys?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Analyze the output of this:

Code: Select all

$predata = explode(", ", $_POST['data']);
$data = array_count_values($predata);
var_dump($predata, $data);
The first array will be the one generated by explode(), the second will be the array generated by array_count_values().
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

Code: Select all

array(6) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "1" [4]=> string(1) "2" [5]=> string(1) "3" } array(3) { [1]=> int(2) [2]=> int(2) [3]=> int(2) }
i don't see how this output helps pick out the most common numbers.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Look at it in the browser's source, not the browser. It should be fairly clear if you used more meaningful values than numbers too.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Lets break this down, shall we?
array_count_values() returns an array using the values of the input array as keys and their frequency in input as values.
array_count_values() returns an array means that whatever you pass to the array_count_values() function, if successfully able to evaluate it, will result in an array coming back to you.

using the values of the input array as keys means that whatever the original array values were (as in $key => $value pairs) will now become the keys of the resultant array.

and their frequency in input as values means that number of times each value was identified in the original array will now be assigned as the value of that same key in the resultant array.

For example, say you have this array:

Code: Select all

<?php $input = array('cat', 'dog', 'cat', 'cat', 'dog', 'big dog', 'angry elephant', 'sexy penguin'); ?>
A dump of this array would look like:

Code: Select all

Array (
  [0] => cat,
  [1] => dog,
  [2] => cat,
  [3] => cat,
  [4] => dog,
  [5] => big dog,
  [6] => angry elephant,
  [7] => sexy penguin
)
Now run it through array_count_values():

Code: Select all

<?php $array = array_count_values($input); ?>
And your new array would look like:

Code: Select all

Array (
  [cat] => 3,
  [dog] => 2,
  [big dog] => 1,
  [angry elephant] => 1,
  [sexy penguin] => 1
)
You catching how that works? On a side note, you can very easily test all of these ideas in your local server. It is something I recommend to all developers. It is very easy to create a PHP script to test this and run it at http://localhost/array-tests.php, you know?
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

feyd wrote:Look at it in the browser's source, not the browser. It should be fairly clear if you used more meaningful values than numbers too.
the thing about the numbers is, i'm trying to create a script where it finds the mean(avg of inputted numbers), median(middle of inputted numbers), and mode(most common inputted number). that is why i'm using numbers.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

tail wrote:
feyd wrote:Look at it in the browser's source, not the browser. It should be fairly clear if you used more meaningful values than numbers too.
the thing about the numbers is, i'm trying to create a script where it finds the mean(avg of inputted numbers), median(middle of inputted numbers), and mode(most common inputted number). that is why i'm using numbers.
Yeah that's true. I think feyd may have meant to test it on something with values other than numbers so you'd be able to see it better, but Everah's post probably cleared that up for you.
Post Reply