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

You might also like

Symfony Forms and Bootstrap Datetimepicker
In this example I'm going to use the spanish locale and the "Europe/Madrid" timezone.First we need...

Multiple class inheritance with Doctrine ODM in Symfony: One collection for multiple document types
When you use Doctrine ORM in Symfony all the data is stored in tables having every row the same columns....

Twig-extensions in Symfony2
Twig comes with multiple filters that provide functions like round, slice, sort, etc. In case you need...

Translate Symfony TimezoneType Field Type
The TimezoneType Field Type Symfony has an specific Field Type to create a Timezone select widget in...

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.