Tag Archives: closures

Sort an array of objects in PHP by field

function cmp($a, $b)
{
    return strcmp($a->name, $b->name);
}

usort($your_data, "cmp");

Or the shorter version using closures:

usort($your_data, function($a, $b)
{
    return strcmp($a->name, $b->name);
});

sort

Ref : http://php.net/manual/en/function.usort.php
http://stackoverflow.com/questions/4282413/php-sort-array-of-objects-by-object-fields