Use the Doctrine ArrayCollection Map and Filter Functions in 2023

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Doctrine ArrayCollection Map and Filter Functions 

Doctrine ArrayCollection is a wrapper for native arrays, having all the methods that PHP defines for their native counterpart. So, this article explores the map and array filter methods for Doctrine collections.

Introduction

ArrayCollection is an implementation of the Collection interface available in one of the Doctrine’s projects. Doctrine is an open-source project that includes PHP libraries focused on dealing with databases – persistence and mapping. There has been a lot of information about Doctrine and ArrayCollection in the introductory article.

This article explores the two crucial functions – map and filter. These functions are available in PHP already, but they won’t work with ArrayCollection as such. To understand why we need to understand the ArrayCollection versus native PHP array.

Once we understand how ArrayCollection is different from the native arrays, we’ll move forward to the map and filter methods that come along with the Collection interface.

The difference between PHP Array and ArrayCollection

PHP array is a core data structure built-in at PHP core. They are not objects of a class. They neither have a method nor do they follow the OOP patterns. However, there are scenarios where the native arrays could be a hassle to deal with. So, we need an array variant with methods and the OOP patterns.

Doctrine ArrayCollection is a wrapper for native PHP arrays. It empowers the native arrays with OOP capabilities. Since the ArrayCollection is an object, it won’t work with PHP functions for native arrays. There is a workaround though but that defeats the purpose of the ArrayCollection.

That’s why the ArrayCollection has all the methods that are available for the native PHP arrays. Methods to add, modify and remove elements. So, map and filter are also defined for ArrayCollection. So, this article explores the map and filter function in the ArrayCollection.

Doctrine ArrayCollection Map Function

The array map function is a higher-order function available across many programming languages. A higher-order function takes another function as an argument, and so does the map function. A map function transforms array elements based on the function’s logic that it receives as an argument.

Doctrine ArrayCollection

PHP has the array_map function for native arrays. As ArrayCollection is an OOP wrapper for the native PHP array, it includes almost all the methods of an array, and the map function is no exception.

ArrayCollection PHP map method

The ArrayCollection has a map method that works similar to the array_map function but here it is invoked as a method of the ArrayCollection object.

<?php
require __DIR__ . '/vendor/autoload.php';
 
use Doctrine\Common\Collections\ArrayCollection;
 
$numbers = new ArrayCollection([1, 2, 3, 4, 5]);
 
$mappedNumbers = $numbers->map(function($num) {
    return $num ** 2; //Square the numbers.
}); // [1, 4, 9, 16, 25]
 
?>

Works as expected. The map method is conveniently called on the numbers ArrayCollection. Here’s a comparable example of the PHP array_map function

PHP array_map vs ArrayCollection map

Here’s the same example using the array_map function.

<?php
require __DIR__ . '/vendor/autoload.php';
 
use Doctrine\Common\Collections\ArrayCollection;
 
$numbers = new ArrayCollection([1, 2, 3, 4, 5]);
 
$mappedNumbers = array_map(function($num) {
    return $num ** 2;
}, $numbers->toArray()); //[1, 4, 9, 16, 25]
 
?>

The toArray function converts the ArrayCollection to the native PHP array. The rest of the flow is the same except that the array_map is not an object’s method.

Doctrine ArrayCollection Filter Function

The array filter is also a higher-order function that filters out array elements based on the criteria defined in the function that it receives. PHP has the array_filter function and it works well for any native PHP array.

array_filter

ArrayCollection PHP filter method

The Doctrine ArrayCollection includes a method to do array filter function. Here’s an example of filtering out negative numbers from an integer ArrayCollection.

<?php
require __DIR__ . '/vendor/autoload.php';
 
use Doctrine\Common\Collections\ArrayCollection;
 
$numbers = new ArrayCollection([-1000, -100, -10, 0, 10, 100, 1000]);
 
$mappedNumbers = $numbers->filter(function($num) {
    return $num > 0; //Keep the positive integers only.
}); //[0, 10, 100, 1000]
 
?>

Perfect! The filter function filter out the negatives and return the ArrayCollection of positive integers only.

PHP array_filter vs ArrayCollection map

Here’s the same example is done through the PHP array_filter function.

<?php
require __DIR__ . '/vendor/autoload.php';
 
use Doctrine\Common\Collections\ArrayCollection;
 
$numbers = new ArrayCollection([-1000, -100, -10, 0, 10, 100, 1000]);
 
$mappedNumbers = array_filter($numbers->toArray(), function($num) {
    return $num > 0; //Keep the positive integers only.
}); //[0, 10, 100, 1000]
?>

Voila! Works just fine. The only difference is that the filter is an ArrayCollection object method while the array_filter is for native PHP arrays.

Conclusion

This article begins by explaining the difference between the ArrayCollection and native PHP arrays. Following that section, it includes examples of the map and filter methods that are defined for the Doctrine ArrayCollection. It also includes comparisons with PHP definitions for native arrays. 

Hopefully, this article helped you in learning something new about PHP. If you are looking for more intuitive and helpful articles and tutorials about PHP, visit FuelingPHP.

Learn More on Symfony

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.