Creating a new array with PHP array_map()

To create a new array from an existing PHP array, you can use the array_map($callback, $array1, $array2, ...) function. The array_map() function iterates over all the elements of an array and returns a new array containing the results of applying the given $callback function to the array elements. The array_map() function does not change the original array but creates a new one. You can pass several arrays to the array_map() function. The number of parameters the $callback function takes must match the number of arrays passed to the array_map(). The first parameter can be a string representing the function name, an array containing an object and a method name, or an anonymous function. The second parameter specifies the array to process. In this PHP Array Map example, we use the array_map() function to loop over an array and return a new array, where a callback function has been applied to each element in the array. Click Execute to run the PHP Array Map Example online and see the result.
Creating a new array with PHP array_map() Execute
<?php
function func($v)
{
  return($v * 2);
}

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

print_r(array_map("func",$array));
?>
Updated: Viewed: 8478 times

What is PHP?

PHP is a server-side scripting language that allows web developers to create dynamic web pages that can interact with databases, process forms, and generate content on the fly. PHP is known for its flexibility and large developer community. PHP supports multiple databases such as SQLite, MySQL, and PostgreSQL. PHP can run on many operating systems, including Linux, Windows, and macOS.

What is an array in PHP?

An array in PHP is a data structure that allows you to store and manipulate a set of values in a single variable. An array is a versatile container that holds a range of values, such as integers, strings, or other arrays, and can be accessed using numeric or string keys. Arrays in PHP can be created using the array() built-in function or the "[..]" square brackets. Each element in an array is identified by a unique index or key, which can be either a number or a string. PHP also includes many built-in functions for working with arrays, such as searching, sorting, converting an array to JSON, splitting a string to array, and determining the length of an array.

How to use the array_map function in PHP?

The array_map() is a PHP function that applies a user-defined function to each element of an array and returns a new array with the modified elements. Following is the syntax of the array_map() function:

PHP array_map() Syntax
array_map(callback, array1, array2, array3 ...)

Where:
  • callback: the callback function that will be applied to each element of the input arrays.
  • array1: the required parameter that specifies the array to process
  • array2, array3 (optional): additional arrays to pass to the callback function
PHP Map Array Example
<?php
function func($v)
{
  return($v * 2);
}

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

print_r(array_map("func",$array));
?>

#output: Array
#(
#    [0] => 10
#    [1] => 8
#    [2] => 4
#    [3] => 4
#    [4] => 6
#)

PHP Map Array Examples

The following are examples using the array_map() method in PHP:

Creating a new array from the original array

The following is an example of creating a new array from the original array using the array_map() method:

PHP Creat Array using array_map()
<?php
$arr = array(1, 2, 3, 4, 5);

$new_arr = array_map(function($value) {
    return $value;
}, $arr);

print_r($new_arr);
?>

#output: Array
#(
#    [0] => 1
#    [1] => 2
#    [2] => 3
#    [3] => 4
#    [4] => 5
#)

Creating a new array by combining the elements of two arrays

The following is an example of creating a new array by concatenating the elements of two arrays using array_map():

PHP
<?php
$arr1 = array("PHP", "Map");
$arr2 = array("Array", "Example");

$new_arr = array_map(function($value1, $value2) {
    return $value1 . " " . $value2;
}, $arr1, $arr2);

print_r($new_arr);
?>

#output: Array
#(
#    [0] => PHP Array
#    [1] => Map Example
#)

See also