php

PHP Array_map() Function

The array_map() function is used in PHP to create a multidimensional array by combining one or more arrays. Another use of this function is to modify each element of the array based on the specific condition. It sends each value to a user-defined callback function that returns a new array after modification. The uses of this function are discussed in this tutorial.

Syntax

array array_map(callable $callback, array $array, array ...$arrays);

The first and second arguments of this function are mandatory. The first argument contains the callback function that will modify the array elements. The second argument contains the array that will be modified. The other arguments are optional and used to store other arrays. It will return a modified array generated by the callback function.

Use of array_map() Function

The uses of the array_map() function for multiple purposes are shown in this part of the tutorial.

Example 1: Create an Array by Removing the Specific Values of the Array
Create a PHP file with the following script to create a new array by removing some specific values from the main array. An array of numeric data has been declared in the script that contains 5 elements. The callback function called callback_func() has declared to create a new array by removing those numbers which are greater than 99 from the numeric array. The return value of the array_map() function will be printed later:

<?php

// Declare an array of numeric data
$num_array = array(78, 45, 300, 98, 690);
// Print the array values
echo "<b>Array values before calling array_map() function:</b><br />";
print_r($num_array);

/*
    Declare callback function
    to remove those numbers from the array
    those are greater than 99
*/
 
function callback_func($value)
{
    // Check the value is greater than 99 or not
    if($value > 99)
    {
        //Remove the value from the array
        unset($value);
    }
    else
        return $value;
}

echo "<br /><b>Array values after calling array_map() function:</b><br />";
// Print the array after removing the empty values
print_r(array_filter(array_map("callback_func", $num_array)));
 
?>

Output:

The following output will appear after executing the previous script. Two elements in the array are more than 99, which are 300 and 690. These two elements have been removed from the output array.

Example 2: Create an Array by Using the Data of Multiple Arrays
Create a PHP file with the following script that will create an array by calculating the power where the base value will be taken from one array, and the power value will be taken from another array. The newly generated array will be printed later:

<?php

// Declare two numeric arrays of equal elements
$num_arr1 = array(2, 3, 5, 8);
$num_arr2 = array(5, 4, 3, 2);

//Print the values of two arrays
echo "<br /><b>The values of two arrays:</b><br />";
print_r($num_arr1);
echo "<br />";
print_r($num_arr2);
echo "<br />";


/*
Define the callback function to calculate the power based
on two array values where the value of the first array
will be the base value and the value of the second array will be the power.
*/

function calculate_power($val1, $val2) {
    // Calculate the power
    return pow($val1, $val2);
}

//Print the array returned by array_map() function
echo "<br /><b>The array values returned by array_map() function:</b><br />";
print_r(array_map("calculate_power", $num_arr1, $num_arr2));

?>

Output:

The following output will appear after executing the previous script:

Example 3: Create an Array by Combining Two Arrays Without a Callback Function
In the previous two examples, the callback function was used in the first argument of the array_map() function. However, this function can be used without a callback function, and the null value was used in the first argument of the function. Create a PHP file with the following script to create a new array by combining the values of two arrays without using any callback function:

<?php

//Declare two arrays of string values
$name_arr1 = array("Sakib", "Sarleez", "Janifer");
$name_arr2 = array("Tania", "Tawsif",  "Zafar", "Fatema");
//Print the values of two arrays
echo "<br /><b>The values of two arrays:</b><br />";
print_r($name_arr1);
echo "<br />";
print_r($name_arr2);
echo "<br />";
 
//Print the array returned by array_map() function
echo "<br /><b>The array values returned by array_map() function:</b><br />";
print_r(array_map(null, $name_arr1, $name_arr2));
?>

Output:

The following output will appear after executing the previous script:

Example 4: Create an Array by Modifying the Numeric Values of an Associative Array
Create a PHP file with the following script to modify the numeric values of the associative array. An associative array of two elements has been declared in the script. The callback function named Modify_array() was declared to add “$” before each number value of the array.

<?php

// Declare an associative array
$assoc_array = ["name" => "Pen", "price" => 10 ];
// Print the array values
echo "<b>Array values before calling array_map() function:</b><br />";
print_r($assoc_array);

/*
Declare a callback function to
add '$' before each numeric value of the array
*/

function Modify_array($val) {
    if(is_numeric($val))
        return "$".$val;
    else
        return $val;
}

echo "<br /><br /><b>Array values after calling array_map() function:</b><br />";
// Print the modified array
print_r(array_map("Modify_array", $assoc_array));

?>

Output:

The following output will appear after executing the previous script. One element of the array contains the numeric value which is 10. The value has changed to “$10” in the output array.

Example 5: Create an Array Based on the Keys and Values of the Associative Array
The callback() function of the array_map() function can take two arguments for the associative array. Create a PHP file with the following script to generate a new array by using the keys and the values of the associative array. The callback function named Modify_assoc_array() will take the key of the array in the first argument and the value of the array in the second argument. The output array will contain the combined values of key and value.

<?php

//Declare an associative array
$marks = [ '01344' => 3.67, '01637' => 3.40, '05632' => 2.99, '03733' => 3.25 ];
// Print the array values
echo "<b>The values of the associative array:</b><br />";
print_r($marks);

/*
Declare a callback function to
create a new array based on the keys and values
of the associative array
*/

function Modify_assoc_array($val1, $val2) {
    return "The CGPA of $val1 is $val2";
}

echo "<br /><br /><b>The array values after calling array_map() function:</b><br />";
// Print the new array values
print_r(array_map('Modify_assoc_array', array_keys($marks), array_values($marks)));
?>

Output:

The following output will appear after executing the previous script:

Conclusion

The different uses of the array_map() function have been explained in this tutorial. Hopefully, this article can help PHP users to know the purpose of using this function and properly apply it in their script. Please check out LinuxHint for more informative articles.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.