Skip to content

Instantly share code, notes, and snippets.

@CDRO
Last active June 21, 2019 07:41
Show Gist options
  • Save CDRO/7f99299329499a0233a126bb044ee3f9 to your computer and use it in GitHub Desktop.
Save CDRO/7f99299329499a0233a126bb044ee3f9 to your computer and use it in GitHub Desktop.
<?php
// fill array
$objects = [];
for($i = 0; $i < 100; ++$i) {
$o = new stdClass;
$o->val = $i * mt_rand() * 1000;
$objects[] = $o;
}
echo 'Before:', PHP_EOL;
print_r($objects);
// php < 7
usort($objects, function($a, $b) {
return $a->val < $b->val;
});
echo 'After:',PHP_EOL;
print_r($objects);
// php >= 7
usort($objects, function($a, $b) {
return $a->val <=> $b->val;
});
// php 7.4, well... that got unreadable!
usort($objects, fn($a, $b) => $a->val <=> $b->val);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment