Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 16, 2021 03:24 pm

How to Use the array_map Function in PHP With Examples


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 to every elements of an array. Apart from that, it also allows you 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 together in 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 to all the elements of an array, the array_map function the a 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 second argument onwards. In our case, we’ve passed two arrays, and the array_map function combines it into a single multidimensional array.


Multiple Arguments


As we’ve 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.



Original Link: https://code.tutsplus.com/tutorials/how-to-use-the-array_map-function-in-php--cms-36856

Share this article:    Share on Facebook
View Full Article

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code