Created
December 9, 2014 20:19
-
-
Save jakefolio/4055bc2606c3c01326f3 to your computer and use it in GitHub Desktop.
This file contains 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
<?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