Register Login

array_map() Function in PHP

Updated Nov 14, 2019

PHP array_map() is an in-built function in PHP, which is used to alter all the elements of one or more arrays according to user-made function. The array_map() function passes each element of an array to the user-made function and then returns the array after modifying all its elements according to the user made function.

Note:

  • User can assign single or multiple arrays to this function
  • This function is compatible with PHP version 4 or above.

Syntax

array_map($userfunction, $array1, $array2...)

Parameter

The PHP array_map() function takes two mandatory parameters $userfunction, $array1 and multiple optional parameters.

Parameter Type Description
$userfunction (mandatory)   Specifies the user-made function according to which the elements of the array will be modified.
$array1 (mandatory) Array specifies the array whose elements are to be modified.
$array2 (optional) Array specifies the array whose elements are to be modified.

Note: User can pass as many array parameters as required.

Return Value

PHP array_map() function return an array containing all the modified elements of the array1 parameter.

Examples of PHP array_map()

Program 1:

<?php
//Array 1
$carsName = ["Maruti","Hyundai","Honda","Ford","Tata","Volkswagen"];
//Array 2
$carPrice = [220000,228000,236000,222000,216000,232000];

$carPrices = array_map(NULL,$carsName,$carPrice);
//Printing the output
var_dump($carPrices);
?>

Output

array(6) { [0]=> array(2) { [0]=> string(6) "Maruti" [1]=> int(220000) } [1]=> array(2) { [0]=> string(7) "Hyundai" [1]=> int(228000) } [2]=> array(2) { [0]=> string(5) "Honda" [1]=> int(236000) } [3]=> array(2) { [0]=> string(4) "Ford" [1]=> int(222000) } [4]=> array(2) { [0]=> string(4) "Tata" [1]=> int(216000) } [5]=> array(2) { [0]=> string(10) "Volkswagen" [1]=> int(232000) } } 

Program 2: 

<?php
//Callback function for array_map function
function getPrice($carName){
    $basePrice = 200000;
    if($carName == 'Maruti'){
        $parcentofBasePrice = 10;
        $carPrice = $basePrice + (($parcentofBasePrice / 100) * $basePrice);
    }else if($carName == 'Hyundai'){
        $parcentofBasePrice = 14;
        $carPrice = $basePrice + (($parcentofBasePrice / 100) * $basePrice);
    }else if($carName == 'Honda'){
        $parcentofBasePrice = 18;
        $carPrice = $basePrice + (($parcentofBasePrice / 100) * $basePrice);
    }else if($carName == 'Ford'){
        $parcentofBasePrice = 11;
        $carPrice = $basePrice + (($parcentofBasePrice / 100) * $basePrice);
    }else if($carName == 'Tata'){
        $parcentofBasePrice = 8;
        $carPrice = $basePrice + (($parcentofBasePrice / 100) * $basePrice);
    }else if($carName == 'Volkswagen'){
        $parcentofBasePrice = 16;
        $carPrice = $basePrice + (($parcentofBasePrice / 100) * $basePrice);
    }
    //Returning the variable value to the array map
    return $carPrice;
}
//Array variable to hold the cars names
$carsName = ["Maruti","Hyundai","Honda","Ford","Tata","Volkswagen"];
//Variable to hold the returned value of array map callback function
$carPrices = array_map('getPrice',$carsName);
//Printing the output
print_r($carPrices);
?> 

Output

Array
(
    [0] => 220000
    [1] => 228000
    [2] => 236000
    [3] => 222000
    [4] => 216000
    [5] => 232000
)
 

 


×