How to use array map in PHP

In this article, I will explain how the array_map() function can be used in PHP.
  • 1901

array_map() function in PHP

  • The array_map() function sends each value of an array to a user-made function, and returns an array with new values, applied by the user-define function.

  • The array_map() function returns an array containing all the elements of array1 after applying the callback function.

Syntax

array_map(function,array1,array2,array3....)

Parameter

  • function function is required parameter. The function defined by user.

  • array1 array1 is required parameter. specifies an array to run through the user defined function.

  • array2 array2 is optional parameter. specifies an array to run through the user defined function.

  • array3 array3 is optional parameter. specifies an array to run through the user defined function.

Example

The following example show to how the array_map() function can be used in PHP.

<html>

<body>

<h3 style="color: darkviolet;">array_map() function example in PHP</h3>

  <?php

    function cube($n)

    {

    return($n * $n * $n);

    }

    $i = array(1, 2, 3, 4, 5);

    $j = array_map("cube", $i);

    print_r($j);

    ?> 

</body>

</html>

 

Output

map.jpg

You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.