I've got an object $contest which is a Symfony entity that has some other entities assigned. I can get them by $content->getEntries();. That will return an object with the lazyloaded db objects. I need to display them in a random order.$tempEntries = $contest->getEntries();$entries = array();if (count($tempEntries)) {    foreach ($tempEntries as $temp) {        $entries[] = $temp;    }    shuffle($entries);}This is the code I use currently. I loop through all elements (if there are any) and then shuffle them. I feel like the looping through them is a wast of resources.Any advice on how to improve this?
Shuffle Symfony entities
php;random;php5;shuffle;symfony2
You can use native shuffling (if you don't use KnpPaginator):$entries = $contest->getEntries()->toArray();shuffle($entries);Also if you need all entities you can write:$entriesCollection = $contest->getEntries();if ($entriesCollection instanceof PersistentCollection) {    $entriesCollection->initialize();}$entries = $entriesCollection->toArray();shuffle($entries);Alternatively you can dig to @ORM\OrderBy and custom DQL functions to use SQL ORDER BY RAND(). Also this approach would work better with KnpPaginatorIn response to comment:Doctrine mostly uses proxies on your classes instead of directly use them, because of lazy loading and optimization purposes, also doctrine has abstraction over basic arrays implementing own Collection interface that has some useful methods like map, exist and so on. PersistentCollection is proxy (lazy) collection implementing Collection interface it can be returned if you just requested some object from repository and trying to use some field that uses an array of objects so in some cases (like that) it's better to force loading of objects via initialize method (don't worry it won't load them twice). Also all objects requested from repository are not your classes they are just proxies extending your entities and implementing Proxy interface so be careful! If something gone wrong in your database layer try to add the following:if ($yourEntityLoadedFromSomeWhere instanceof Proxy) {    $yourEntityLoadedFromSomeWhere->__load();}