Filtering and Sorting a Doctrine Arraycollection

ArrayCollection object has so many useful in-build methods, that makes ArrayCollection superior comparable to normal PHP array. This post is about how to filter and sort an ArrayCollection so output of elements can be in certain order or filtered.

Filter

To filter we can use ArrayColleciton in-build filter method, which takes a predicate and returns all the elements satisfied by the predicate provided, keeping the order of the elements intact. A code example would be nice.

Code Example :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php

/**
 * @var \Doctrine\Common\Collections\Collection
 */
$locations; // ArrayCollection() variable that has $location object in its element.

// To filter active locations.
$activeLocations = $locations->filter(function($location) {
    return $location->getStatus(); // filter based on status.
});
// Now $activeLocations holds all locaitons from $locations those have an active locaiton.

?>


Sort

ArrayCollection doesn't have any in-built sort methods, Though there is a matching method which can work over an ArrayCollection with help of Doctrine\Common\Collections\Criteria, but its limited to one-to-many use cases. A many-to-many association can't enjoy such usage.

Thus below is a snippet that gets Array iterator from ArrayCollection and sorts data accordingly before making a fresh ArrayCollection with sorted elements.

Code Example :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php

$amenities = $entity->getAmenities(); // ArrayCollection data.

// Collect an array iterator.
$iterator = $amenities->getIterator();

// Do sort the new iterator.
$iterator->uasort(function ($a, $b) {
    return ($a->getPrice() > $b->getPrice()) ? -1 : 1;
});

// pass sorted array to a new ArrayCollection.
$amenitiesSorted = new \Doctrine\Common\Collections\ArrayCollection(iterator_to_array($iterator));

?>

Comments

Unknown said…
Thank you! Your filter explanation helped me.
Unknown said…
Thank you. Your filter explanation helped me.
Jeet's Blog said…
Glad, It helped you. :)
kevingeorge said…

I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.




Data Science Training in Bangalore

shiny said…
Thank you so much for your brief explanation for sorting concept in array which used to be very complicated for beginners.
apple mobile service centre in chennai
janitha said…
I just found this blog and have high hopes for it to continue. Keep up the great work, its hard to find good ones. I have added to my favorites. Thank You.
big data course malaysia
jaanu said…
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
big data course
Padminiprwatech said…
Thanks for sharing useful information. I learned something new from your bog. It's very interesting and informative. keep updating. If you are looking for any Data science related information, please visit our website bigdata training institute in bangalore.
devi said…
You are in point of fact a just right webmaster. The website loading speed is amazing. It kind of feels that you're doing any distinctive trick. Moreover, The contents are masterpiece. you have done a fantastic activity on this subject!
Data Science Training In Chennai

Data Science Online Training In Chennai

Data Science Training In Bangalore

Data Science Training In Hyderabad

Data Science Training In Coimbatore

Data Science Training

Data Science Online Training
Ava Volkov said…
Your ability to give a balanced viewpoint that takes into account other viewpoints and addresses potential problems stands out above all else. Blue Prism Training
Ava Volkov said…
You have recently made some very outstanding contributions. Your breadth of knowledge, astute analysis, and lucidity of expression are admirable. Your knowledge enriches our conversations and offers priceless direction. Blue Prism Certification
Ava Volkov said…
You have recently made some very outstanding contributions. Your breadth of knowledge, astute analysis, and lucidity of expression are admirable. Your knowledge enriches our conversations and offers priceless direction. Blue Prism Course
In Digital Blog said…
Thank you! very good explanation. are u interest in seo SEO Course in Dehradun
HarishKIT said…
Great post! The clear examples for filtering and sorting ArrayCollections are super helpful. I appreciate the practical approach and detailed explanation. Thanks for sharing these valuable tips!

online internship | internship in chennai | online internship for students with certificate | bca internship | internship for bca students | sql internship | online internship for btech students | internship for 1st year engineering students

Popular posts from this blog

Doctrine mapping (ORM) with Database (DAL), Unidirectional vs Bidirectional.

Symfony 2 How to keep cloned entity properties unchanged on change of main entity.