Page 2 of 3

Posted: Thu May 31, 2007 8:13 pm
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];

Posted: Thu May 31, 2007 8:47 pm
by tail
thanks a lot

Posted: Thu May 31, 2007 9:33 pm
by tail
is there a way to extract which number appears the most?

Posted: Thu May 31, 2007 9:35 pm
by feyd

Posted: Thu May 31, 2007 9:41 pm
by tail
i'm confused by this. how would this extract the most common number in the array?

Posted: Thu May 31, 2007 10:09 pm
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.

Posted: Thu May 31, 2007 10:21 pm
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.'';

Posted: Thu May 31, 2007 10:44 pm
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.

Posted: Thu May 31, 2007 10:50 pm
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?

Posted: Thu May 31, 2007 10:59 pm
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().

Posted: Fri Jun 01, 2007 10:59 am
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.

Posted: Fri Jun 01, 2007 11:10 am
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.

Posted: Fri Jun 01, 2007 11:19 am
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?

Posted: Fri Jun 01, 2007 3:41 pm
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.

Posted: Fri Jun 01, 2007 3:44 pm
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.