AppDividend
Latest Code Tutorials

PHP Array Map Example | array_map() Function Tutorial

0

PHP Array Map Example | array_map() Function Tutorial is today’s topic. The array_map() is the inbuilt function in PHP, and it helps to modify all the elements one or more arrays according to some user specific condition in a straightforward manner. The array_map() function sends each value of an array to a user-defined function and gets an array with new values applied by the user-defined function. It applies the callback to the elements of the given arrays.

PHP Array Map Example

The syntax of PHP array_map function is following.

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

The myfunction parameter is required, and it is the name of the User Defined Function.

An array1 parameter is a required parameter, and it specified an array.

The array2, array3 are optional parameters, and it specified the arrays as well.

It returns an array containing the values of array1, after applying the user-made function to each one.

See the following example.

<?php

// app.php

function square($x)
{
  return($x * $x);
}
$arrA = array(19, 21, 46, 29);
$arrO = array_map("square",$arrA);
print_r($arrO);

We have defined one array and then pass that array to the array_map function, and that function returns the square of all the items of the array and forms a new array with that values. The output of the above code is the square of all the items. See the below output.

 

PHP Array Map Example

Creating an array of arrays using array_map()

We can also use an array_map() function in PHP to create the array of arrays. If we want to do this, we have to pass null as a parameter in place of the functionName parameter and the list of arrays to create an array of arrays. See the following example.

<?php

// app.php

$arrA = array(19, 21, 46, 29);
$arrB = array('K', 'K', 'A', 'V');
$arrO = array_map(null, $arrA, $arrB);
print_r($arrO);

The output is following.

 

Creating an array of arrays using array_map()

The array_map() Using Multiple Arrays

Let’s take three arrays and pass those arrays to an array_map function and see the output.

<?php

// app.php

function subtractAdd($a, $b, $c) {
  return $a - $b + $c;
}

$arrA = array(19, 21, 46, 29);
$arrB = array(18, 19, 20, 21);
$arrC = array(1, 2, 26, 8);
$arrO = array_map("subtractAdd", $arrA, $arrB, $arrC);
print_r($arrO);

The output is following.

 

The array_map() Using Multiple Arrays

The array_map() with string keys

Let’s pass the associative array to the array_map() function and see the output.

<?php

// app.php

function stormBreaker($a) {
  return $a."***".$a;
}

$arrA = ["name" => "Krunal", "age" => 26 ];
$arrO = array_map("stormBreaker", $arrA);
print_r($arrO);
The output is following.

 

The array_map() with string keys

array_map() using a lambda function

Lambda function means it has no name. See the following example.

<?php

// app.php

$func = function($value) {
  return $value * 2;
};

print_r(array_map($func, range(1, 5)));

We have used the $func as lambda function and range() function to provide the value between 1 to 5. See the below output.

 

array_map() using a lambda function

Finally, PHP Array Map Example | array_map() Function Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.