Skip to content

Instantly share code, notes, and snippets.

@bondarewicz
Created April 2, 2014 11:10
Show Gist options
  • Select an option

  • Save bondarewicz/9932113 to your computer and use it in GitHub Desktop.

Select an option

Save bondarewicz/9932113 to your computer and use it in GitHub Desktop.
PHP: return any specific (2nd or 3rd largest) number from array
<?php
function whats_largest($arr, $idx)
{
sort($arr, SORT_NUMERIC);
return($arr[count($arr) - intval($idx)]);
}
$array = array();
for ($i = 1; $i <= 10; $i++) {
array_push($array, rand ( 1, 1000 ));
}
print_r($array);
echo whats_largest($array,2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment