How to use  array_map() function in PHP?

Summary

In this post, we will learn PHP array_map()’s inner workings, syntax, and usage cases to illustrate how valuable they are in a variety of contexts.

Introduction

array_map() is a built-in function of PHP that takes one or more arrays and applies a given callback function to each element.The result is an array containing the modified values, preserving the array keys.

Syntax

The syntax of array_map() in PHP is :

array_map($callbackFunction, $array1, $array2…)

Parameters

$callbackFunction: The function to apply to each element of the input arrays. Type should be function.

$array1: The array to be processed. Type should be Array.

$array2… : Additional arrays, if any, to be processed using the same callback function. Type should be Array.

Examples

Example 1: Doubling the Values

Suppose we have an array of numbers and we want to double each value. Let’s see how array_map() can accomplish this:

<?php
$numbers = [2, 4, 6, 8, 10];

$doubledNumbers = array_map(function($num) {

    return $num * 2;

}, $numbers);
?>

Result

Array
(

    [0] => 4
    [1] => 8
    [2] => 12
    [3] => 16
    [4] => 20

)

Example 2: Combining Arrays

Sometimes, we might need to combine multiple arrays element-wise. array_map() makes this task effortless:

<?php

$users = ["Alice", "Bob", "Charlie"];

$scores = [85, 92, 78];

$combined = array_map(function($users, $score) {

    return "$users scored $score";

}, $users, $scores);

?>

Result

Array
(

    [0] => Alice scored 85
    [1] => Bob scored 92
    [2] => Charlie scored 78

)

Example 3: Custom Callback Function

You can use your own custom functions providing endless possibilities:

<?php

$temperatures = [25, 30, 18, 22];

function celsiusToFahrenheit($celsius) {

    return ($celsius * 9/5) + 32;

}
$fahrenheitTemps = array_map('celsiusToFahrenheit', $temperatures);
?>

Result

Array
(
    [0] => 77
    [1] => 86
    [2] => 64.4
    [3] => 71.6
)

Example 4: Handling Multiple Arrays

array_map() can work with multiple input arrays, as long as the callback can handle the same number of arguments:

<?php

$numbers1 = [1, 2, 3];

$numbers2 = [4, 5, 6];

$sums = array_map(function($num1, $num2) {

    return $num1 + $num2;

}, $numbers1, $numbers2);

?>

Result 

Array
(
    [0] => 5
    [1] => 7
    [2] => 9
)

Alternatives

array_walk() or foreach loops are the alternatives of array_map().

Frequently Asked Questions

Q1. What is the use of array_map() in PHP?

Ans: array_map() in PHP applies a function to each element of an array, creating a new array with the results. It simplifies element-wise transformations and data manipulation.

Q2. How to create new array in array_map PHP?

Ans: To create a new array using the array_map() method in PHP, you need to provide the existing arrays and the callback function. The callback function returns a new array for each element, resulting in a changed array as output.

Q3. What is the difference between array_map() and array_shift()?

Ans: PHP array_map() is focused on transforming array elements and creating a new array, while array_shift() is used to extract and manipulate elements directly within the original array.

Conclusion

array_map() is one of the most versatile and powerful functions for manipulating the array. However, in performance-critical cases, it is better to use different methods. Take advantage of the power of the this function and take your PHP programming to the next level.

%d bloggers like this: