Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Advertisement
  1. Code
  2. PHP

How to Use the array_map Function in PHP With Examples

by
Difficulty:BeginnerLength:ShortLanguages:

In this quick article, we’ll discuss the array_map function in PHP. Basically, we’ll go through the syntax of the array_map function and demonstrate how to use it with real-world examples.

The array_map function allows you to apply a callback function to transform elements of an array. It’s really useful when you want to perform a specific operation on every element of an array. Apart from that, it also allows you to combine multiple arrays into a single multidimensional array.

Instead of iterating through all the array elements with the foreach construct to perform a specific operation on them, you should prefer the array_map function, which is built specifically for this.

Syntax of the array_map Function

In this section, we’ll go through the syntax of the array_map function to understand how it works.

Let’s have a look at the syntax of the array_map function:

The first argument is the callback function, which is called when the array_map function iterates over the elements of an array. It could be a user-defined function or a class method in the callable form. 

The second argument is the source array on which the callback function will run.

In most cases, you would only need these two arguments. But the array_map function allows you to pass additional array arguments that are passed as arguments to the callback function. They will all be combined into one multi-dimensional array in the output. It’s interesting that you can pass null as the callback, and array_map just performs a zip operation on the source arrays. I'll demonstrate this with a couple of real-world examples in the next section.

The array_map function returns an array of all values processed by the callback function.

In the next section, we’ll go through a couple of real-world examples to understand how the array_map function works.

Real-World Examples

In this section, we’ll discuss how to use the array_map function in different ways.

How to Lowercase an Array

This is one of the most basic and useful examples. When you want to perform a specific operation on all the elements of an array, the array_map function is the way to go!

As you can see, we’ve passed the lowercase callable in the first argument, so this converts all the elements of an array to lowercase.

How to Use array_map With a Class

In this example, we’ll see how you can apply a class method to all the elements of an array.

In the above example, we’ve passed array($objUtility, 'formatPrice') as a callable in the first argument. Basically, we’re just checking if an array element is prefixed with the $ sign, and if it doesn’t, we’re prepending it.

Zip Operation on Multiple Arrays

Recall that in the syntax of the array_map function, you can pass null as the first argument instead of a callable. In this section, we’ll see how that works with an example.

As we have passed the null value in the first argument, it performs a zip operation on all the arrays that are passed from the second argument onwards. In our case, we’ve passed two arrays, and the array_map function combines them into a single multidimensional array.

Multiple Arguments

As we discussed earlier, you can pass multiple arrays to array_map. In this section, we’ll see how you can use that with a callback function.

In the above example, the createEmployeeObject callable generates an array of Employee objects.

Conclusion

In this article, we discussed the basics of the array_map function in PHP. We also went through a couple of real-world examples to demonstrate the power of the array_map function.

Advertisement
Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.