PHP Array array_map() Function (With Examples)

In this tutorial, we will learn about the PHP array_map() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP array_map() function

The array_map() function is used to apply operations on each array values (elements) based on the given function, it sends each value of an array to the given function and returns a new array with the calculated values.

Syntax

The syntax of the array_map() function:

array_map(function, array1, [array2], ...);

Parameters

The parameters of the array_map() function:

  • function is the name of the function, that will be used to apply operation on each value of the given array.
  • array1 is an array on which we have to perform the operation.
  • array2, ... are optional parameters, we can specify multiple arrays too.

Return Value

The return type of this method is array, it returns an array containing the results of applying the callback function to the corresponding value of array (and arrays if more arrays are provided) used as arguments for the callback. [Source]

Sample Input/Output

Input:
$arr = array(10, 20, 30, 40, 50);

Function:
function getSquare($value)
{
    return ($value*$value);
}

Function call:
array_map("getSquare", $arr);

Output:
Array
(    
    [0] => 100   
    [1] => 400   
    [2] => 900   
    [3] => 1600  
    [4] => 2500  
)

PHP array_map() Function Example 1

Getting the squares and cubes of the all values.

<?php
    //functions
    function getSquare($value)
    {
        return ($value*$value);
    }
    function getCube($value)
    {
        return ($value*$value*$value);
    }
    
    //array
    $arr = array(10, 20, 30, 40, 50);
    
    //new array of squares of the array's values
    $arr_sqr = array_map("getSquare", $arr);
    //new array of squares of the array's values
    $arr_cube = array_map("getCube", $arr);    
    
    //printing
    print_r ($arr_sqr);
    print_r ($arr_cube);
?>

Output

The output of the above example is:

Array
(    
    [0] => 100   
    [1] => 400   
    [2] => 900   
    [3] => 1600  
    [4] => 2500  
)    
Array
(    
    [0] => 1000  
    [1] => 8000  
    [2] => 27000 
    [3] => 64000 
    [4] => 125000
) 

PHP array_map() Function Example 2

Finding sum of the values of two arrays.

<?php
    //function to add values of two arrays
    function addValues($value1, $value2)
    {
        return ($value1 + $value2);
    }
    
    //arrays
    $arr1 = array(10, 20, 30, 40, 50);
    $arr2 = array(100, 200, 300, 400, 500);
    
    $result = array_map("addValues", $arr1, $arr2);
    print_r ($result);
?>

Output

The output of the above example is:

Array
(    
    [0] => 110
    [1] => 220
    [2] => 330
    [3] => 440
    [4] => 550
)

To understand the above examples, you should have the basic knowledge of the following PHP topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.