PHP array_map is a built-in PHP function that modifies arrays based on specific conditions. It applies a function to each element of an array and generates a new array with updated values.
This article will explore its syntax, usage, and examples. Let’s see the basic pattern for it with following example.
<?php
function arr_map_func( $element ) {
if ( $element > 15 && $element < 24) {
$element = $element ." (✓)";
}
return $element;
}
$ages = array(
10, 15, 11, 18, 17, 15, 16, 35, 30,
20, 25, 22, 23, 24, 50, 45, 40, 100,
90, 44, 41, 43, 38, 39, 31, 28, 5, 6
);
$youths_age = array_map( 'arr_map_func', $ages );
echo "<pre>";
print_r($youths_age);
echo "</pre>";
?>
Executing this example will output the following result.
Array ( [0] => 10 [1] => 15 [2] => 11 [3] => 18 (✓) [4] => 17 (✓) [5] => 15 [6] => 16 (✓) [7] => 35 [8] => 30 [9] => 20 (✓) [10] => 25 [11] => 22 (✓) [12] => 23 (✓) [13] => 24 [14] => 50 [15] => 45 [16] => 40 [17] => 100 [18] => 90 [19] => 44 [20] => 41 [21] => 43 [22] => 38 [23] => 39 [24] => 31 [25] => 28 [26] => 5 [27] => 6 )
The PHP array_map
function is a built-in function in PHP that can be used to update and modify all elements of an array based on a condition that is defined within the function.
Parameters and the Function Return
The PHP array_map
function requires two arguments: the main array and the function to be performed. Additionally, it can accept an unlimited number of arguments of the array data type.
So, the basic syntax for using the array_map function is as follows.
array_map( FUNCTION, REQUIRED_ARRAY, ARRAY1, ARRAY2, ARRAY3, .. );
In the following section, I will provide an explanation of how this function operates.
How the PHP array_map Works
It is worth noting that the array_map
function accepts two required parameters, but it is also possible to provide additional optional parameters of the array data type.
It’s important to note that the array_map
function allows for the array fields to be passed into the function that is defined as the first argument. This permits each element to be modified individually, with the function returning a result for each one.
function userdefined_map_func( $element_of_array ) { // Access the element of array here to modify the value } array_map( 'userdefined_map_func', ARRAY );
The array_map function sends the array to the defined function using the “$element_of_array
” argument. This may appear as though a loop is being performed on all array elements, but instead, a value is returned for each field.
Furthermore, it’s possible to compare two or more arrays together using the array_map function based on the position of the array elements.
This means that the function compares the element at index 0 of one array with the element at index 0 of another array, and so on, for all corresponding elements at each index.
The basic syntax for using array_map
to compare arrays is as follows.
function func_name( $array1_elem, $array2_elem ) { // Access the element of array here to modify the value if( $array1_elem === $array2_elem ) { return "Passed" } return "Not Equal"; } array_map( 'func_name', $array1, $array2 );
In this case, $array1_elem
represents one row of the first array, while $array2_elem
represents another row of the other array.
The two rows are at the same index, which enables the function to compare them.
In any case, the following section provides several examples to illustrate this concept.
PHP array_map Examples
One example of using the built-in PHP function is to convert the cases of array values to lowercase.
$array = array(
"SDAD",
"SDAs",
"valUe"
);
$arr = array_map( "strtolower", $array );
echo "<pre>";
print_r($arr);
echo "</pre>";
The output:
Array ( [0] => montasser [1] => moneer [2] => mohamed [3] => fadi )
For another example, we can use the same array as the previous example to convert the first letter of each value to capital while keeping the remaining letters in lowercase.
$arr = array_map( "ucfirst", $array );
echo "<pre>";
print_r($arr);
echo "</pre>";
The output:
Array ( [0] => Montasser [1] => Moneer [2] => Mohamed [3] => Fadi )
An example of using the array_map function involves utilizing an anonymous function to display the result of exponentiation.
<?php
$callback = function($row) {
$exponent = $row ** $row;
return $exponent;
};
$array = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 );
$exponent = array_map( $callback, $array );
echo "<pre>";
print_r($exponent);
echo "</pre>";
?>
The output:
Array ( [0] => 1 [1] => 4 [2] => 27 [3] => 256 [4] => 3125 [5] => 46656 [6] => 823543 [7] => 16777216 [8] => 387420489 [9] => 10000000000 )
An example for the user defined function.
<?php
function user_defined_func( $row ) {
if ( $row == 'peter' ) {
$row .= " ====> is not valid name ( X )";
}
return $row;
}
$array = array ( "Ahmed", "Mohamed", "Michael", "peter", "Tarek", "Khaled" );
$modified = array_map( 'user_defined_func', $array );
echo "<pre>";
print_r($modified);
echo "</pre>";
?>
The output:
Array ( [0] => Ahmed [1] => Mohamed [2] => Michael [3] => peter ====> is not valid name ( X ) [4] => Tarek [5] => Khaled )
An additional example involves demonstrating the use of a user-defined function with the array_map
function.
<?php
$array1 = array( 5, 6, 100, 20, 142, 58);
$array2 = array( 10, 6, 100, 19, 124, 58);
$callback = function( $arr1_row, $arr2_row ) {
$return = "{$arr1_row} == {$arr2_row} = X";
if ( $arr1_row === $arr2_row ) {
$return = "{$arr1_row} == {$arr2_row} = ✓";
}
return $return;
};
$modified = array_map($callback, $array1, $array2);
echo "<pre>";
print_r($modified);
echo "</pre>";
?>
The output:
Array ( [0] => 5 == 10 = X [1] => 6 == 6 = ✓ [2] => 100 == 100 = ✓ [3] => 20 == 19 = X [4] => 142 == 124 = X [5] => 58 == 58 = ✓ )
Wrapping Up
If you have a big array or whatever and you need to change some values according to a condition you have to use the array_map function. The basic expression would be like the below.
When working with a large array or similar data structure, and you need to modify certain values based on a specific condition, utilizing the array_map
function can prove to be a useful approach. The basic syntax for using the function is as follows.
array_map( FUNCTION, REQUIRED_ARRAY, ARRAY1, ARRAY2, .. );
The array_map
function returns a new array with modified data based on the applied function.