The PHP array_map function would be written as the 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>";
?>

This example will print the below 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
)

So, The PHP array_map function is a PHP built-in function used to modify and update all elements of the array according to a condition inside the function.

The PHP array_map() Parameters and the Function Return

The PHP array_map function has two required arguments to be the function and the main array and unlimited arguments with array data type.

So the basic expression for the array_map would be like the below.

array_map( FUNCTION, REQUIRED_ARRAY, ARRAY1, ARRAY2, ARRAY3, .. );

In the next section, I will explain how it works.

How the PHP array_map Works

Actually, the array_map accepts two required parameters and you can add more optional parameters with array data types.

So, the array fields can be sent into the function that already passed in the first argument and that to modify each element separately but the function should return 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 );

So the array is sent to the function though this $element_of_array argument, that seems you are doing a loop for all array elements, but here it would return a value for each field.

Also, you can compare two or more arrays together through the array_map according to the position of array elements which mean index 0 equal to index 0 in the other array, index 1 equal to index 1 in the other array. And so on.

The basic expression would be like the below.

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 );

So, the $array1_elem is one row of the first array and $array2_elem is another row of the other array but two both are in the same index which allow you compare them together through the passed function.

Anyway, the following section shows you examples for that.

PHP array_map Examples

Converting the array value cases to small letter using built-in PHP function.

$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, I will use the same array of the above example to convert the first letter only into capital.

$arr = array_map( "ucfirst", $array );

echo "<pre>";
print_r($arr);
echo "</pre>";

The output:

Array
(
    [0] => Montasser
    [1] => Moneer
    [2] => Mohamed
    [3] => Fadi
)

Using the anonymous function with the array_map to display the result of the 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
)

Also, you can use the array_map to compare values of two or more arrays together but in the same index. For example.

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

array_map( FUNCTION, REQUIRED_ARRAY, ARRAY1, ARRAY2, .. );

This function returns another array with new changed data.