array_map() , php array map function

The “array_map()” function sends each value of an array to a function, then returns an array with new value that are modified inside the function. It allows us to run all of our array elements through a callback function we specify. This is a good way to filter, evaluate, or change array values quickly to transform your array values using custom or predefined function you specify.

Syntax

Syntax:

    array_map(function,array1,array2,array3)

In the above syntax the “array1” is an mandatory field, “array2”, “array3” are optional, “function” is also a mandatory field, to which all the values of “array1” is send to.

Example:

   <?php
   function get_square($num)
    {

      return $num*$num;
     }
   $array=array("1","4","5");
   $result = (array_map("get_square",$array));
   foreach($result as $val){
           echo 'The square of the each array element is :'.$val.<br/>'';
}
 ?>

Result:

The square of the each array element is :1
The square of the each array element is :16
The square of the each array element is :25

Similar Example: