Created
December 18, 2025 11:29
-
-
Save faai5200/ce31396676f3036fbfc560041c619654 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This function will sort the records by age desc | |
| function sortByAgeDesc($arrayOfObjects) { | |
| usort($arrayOfObjects, function($a, $b) { | |
| return $b['age'] <=> $a['age']; | |
| }); | |
| return $arrayOfObjects; | |
| } | |
| // This function will return just the ages: | |
| function getAges($arrayOfObjects) { | |
| return array_map(function($item) { | |
| return (int)$item['age']; | |
| }, $arrayOfObjects); | |
| } | |
| // This function converts to Array of arrays while keeping the age as key | |
| function convertToArrayByAgeKey($arrayOfObjects){ | |
| $return = []; | |
| foreach ($arrayOfObjects as $object){ | |
| $return[$object['age']] = $item; | |
| } | |
| return $return; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment