AppDividend
Latest Code Tutorials

PHP Array Map Example | PHP array_map() Function

0

PHP array_map() is an inbuilt function that helps to modify all the elements one or more arrays according to some user-specific conditions 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

PHP array map is the inbuilt function in PHP, and it helps to modify all items one or more arrays according to some user-defined custom condition in a straightforward manner.

The syntax of the PHP array_map function is the 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 array of arrays using array_map()

We can also use an array_map() function in PHP to create an array of arrays.

If we want to do this, then 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 the following.

Creating an array of arrays using array_map()

PHP 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 the following.

The array_map() Using Multiple Arrays

PHP 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 the following.

The array_map() with string keys

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

Difference between array_map(), array_walk() and array_filter()

What exactly is the difference between array_map, array_walk, and array_filter? Well, see the below answer.

Changing Values:

The array_map() function cannot change the values inside the input array(s) while array_walk() can; in particular, array_map() function never changes its arguments.

Array Keys Access:

The array_map() function cannot operate with the array keys, PHP array_walk() function can.

Return Value:

The array_map() function returns the new array, array_walk() function only returns true.

Hence, if you don’t want to create the array as a result of traversing one array, you should use array_walk() function.

Iterating Multiple Arrays:

The array_map() function also can receive the arbitrary number of arrays, and it can iterate over them in parallel, while array_walk() function operates only on one.

Passing Arbitrary Data to Callback:

PHP array_walk() function can receive the extra arbitrary parameter to pass to the callback. This mostly irrelevant since PHP 5.3.

Length of Returned Array:

The resulting array of array_map() function has the same length as that of the largest input array; array_walk() function does not return an array, but at the same time it cannot alter the number of elements of an original array;

PHP array_filter() function picks only a subset of the elements of the array according to a filtering function. It does preserve the keys.

Finally, the PHP array_map() function example is over.

Related Posts

PHP array_filter()

PHP array_keys()

PHP array_push()

PHP array_pop()

PHP array_shift()

Leave A Reply

Your email address will not be published.

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