3v4l.org

run code in 300+ PHP versions simultaneously
<?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; }
Output for git.master, git.master_jit, rfc.property-hooks
James: 3 Charles: 2 John: 1

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
36.77 ms | 401 KiB | 8 Q