Skip to content

Instantly share code, notes, and snippets.

@pepegar
Created June 7, 2013 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pepegar/5729411 to your computer and use it in GitHub Desktop.
Save pepegar/5729411 to your computer and use it in GitHub Desktop.
Get Extremes of integer array
<?php
function getExtremes($test)
{
$sum = array_sum($test);
$average = $sum / count($test);
$deviations = array();
$ret = array();
for ($i = 0; $i < count($test); $i++)
{
$deviation = $test[$i] - $average;
$deviation = abs($deviation);
$deviations[$i] = $deviation;
}
// Extreme value
$max = max($deviations);
foreach ($deviations as $deviation)
{
if ($deviation == $max) {
$ret[] = $deviation;
}
}
if (count($ret) == 1) {
$ret = $ret[0];
}
var_dump($ret);
}
// Set of arrays to test
$tests = array(
array( 1,2,3,4,5,6,7,8,89,9,0,45,3,-1,-1234,-1234,234,1234,1234, -234, 345, -345 ),
array( 1,2,3,4,5,6,7,8,89,9,0,45,3,-1,-1234),
);
// Loop the arrays to test each of them
foreach ($tests as $test)
{
getExtremes($test);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment