Skip to content

Instantly share code, notes, and snippets.

@faai5200
Created December 18, 2025 11:29
Show Gist options
  • Select an option

  • Save faai5200/ce31396676f3036fbfc560041c619654 to your computer and use it in GitHub Desktop.

Select an option

Save faai5200/ce31396676f3036fbfc560041c619654 to your computer and use it in GitHub Desktop.
// 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