Skip to content

Instantly share code, notes, and snippets.

@jakefolio
Created December 9, 2014 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakefolio/4055bc2606c3c01326f3 to your computer and use it in GitHub Desktop.
Save jakefolio/4055bc2606c3c01326f3 to your computer and use it in GitHub Desktop.
<?php
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$obj1->name = "Joe";
$obj2->name = "Jane";
$obj3->name = "Johnny";
$collection = [$obj1, $obj2, $obj3];
// Order by longest name property
usort($collection, function($a, $b) {
$aLength = strlen($a->name);
$bLength = strlen($b->name);
if ($aLength == $bLength) {
return 0;
}
return ($aLength > $bLength) ? -1 : 1;
});
echo $collection[0]->name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment