Home » PHP

PHP array_map() function with example

PHP array_map() function: Here, we are going to learn about the array_map() function with example in PHP.
Submitted by IncludeHelp, on February 22, 2019

PHP array_map() function

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:

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

Here,

  • 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.

Examples:

    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 code 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

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

PHP code 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

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.