@ 2021-08-05T10:44:52Z <?php
// groupA:
// James:
// gpa: 3
// age: 13
// Charles:
// gpa: 4
// age: 15
// John:
// gpa: 2
// age: 17
$groupA = [
'James' => [
'gpa' => 3,
'age' => 13
],
'Charles' => [
'gpa' => 4,
'age' => 15
],
'John' => [
'gpa' => 2,
'age' => 17
]
];
/**
* @throws \InvalidArgumentException
*/
function getPlace(string $for, string $compareValueKey, array $haystack, int $flags = SORT_REGULAR): int
{
if(empty($haystack) || !isset($haystack[$for]))
{
throw new \InvalidArgumentException("empty haystack array given (or key '$for') was not found");
}
$sort = array_map(fn(array $el) => $el[$compareValueKey], $haystack);
arsort($sort, $flags);
$place = array_search($for, $places = array_keys($sort), true);
if(!is_int($place))
{
throw new \RuntimeException("could not locate position for key '$for' in given array: " . trim(implode(', ', $places)));
}
return $place + 1;
}
$persons = array_keys($groupA);
foreach($persons as $person)
{
echo $person . ': ' . getPlace($person, 'age', $groupA) . PHP_EOL;
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.3.7 0.014 0.003 16.63 8.3.6 0.007 0.011 16.50 8.3.5 0.007 0.010 16.30 8.3.4 0.008 0.008 18.89 8.3.3 0.010 0.010 19.02 8.3.2 0.005 0.003 24.18 8.3.1 0.004 0.004 24.66 8.3.0 0.005 0.003 26.16 8.2.19 0.008 0.008 18.41 8.2.18 0.011 0.011 25.92 8.2.17 0.006 0.009 19.04 8.2.16 0.012 0.009 22.96 8.2.15 0.000 0.007 25.66 8.2.14 0.004 0.004 24.66 8.2.13 0.004 0.004 26.16 8.2.12 0.012 0.003 26.16 8.2.11 0.006 0.003 20.12 8.2.10 0.000 0.009 19.23 8.1.28 0.010 0.010 25.92 8.1.27 0.004 0.004 23.99 8.1.26 0.000 0.007 26.35 8.1.25 0.000 0.008 28.09 8.1.24 0.000 0.011 18.47 8.1.23 0.006 0.003 22.14 8.0.9 0.002 0.006 16.88
preferences:dark mode live preview
28.24 ms | 401 KiB | 5 Q