PHP - Ds Map::apply() Function



The PHPDs\Map::apply()function is used to update all values by applying a callback function to each value. This function accepts a callback function that should return replace the original value of the current map.

Syntax

Following is the syntax of the PHP Ds\Map::apply() function −

public Ds\Map::apply(callable $callback): void

Parameters

Following is the function of this function −

  • callback − A callable function to apply on each element.

Following is the syntax of the callback function −

callback(mixed $key, mixed $value): mixed

Return value

This function doesn't return any value.

Example 1

The following is the basic example of the PHP Ds\Map::apply() function −

<?php 
   $map = new \Ds\Map([10, 20, 30, 40, 50]);
   echo "The map elements are: \n";
   print_r($map);
   echo "The map after applying the callback function: \n";
   $func = function($key, $value) { 
      return $value + 10;
   }; 
   $map->apply($func); 
   print_r($map);
?>

Output

The above program produces the following output −

The map elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => 10
        )

    [1] => Ds\Pair Object
        (
            [key] => 1
            [value] => 20
        )

    [2] => Ds\Pair Object
        (
            [key] => 2
            [value] => 30
        )

    [3] => Ds\Pair Object
        (
            [key] => 3
            [value] => 40
        )

    [4] => Ds\Pair Object
        (
            [key] => 4
            [value] => 50
        )

)
The map after applying the callback function:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => 20
        )

    [1] => Ds\Pair Object
        (
            [key] => 1
            [value] => 30
        )

    [2] => Ds\Pair Object
        (
            [key] => 2
            [value] => 40
        )

    [3] => Ds\Pair Object
        (
            [key] => 3
            [value] => 50
        )

    [4] => Ds\Pair Object
        (
            [key] => 4
            [value] => 60
        )

)

Example 2

Following is another example of the PHPDs\Map::apply()function. We use this function to update all elements by applying a callback function on each value of this map (["1" => "Tutorials", "2" => "Point", "3" => "India"])−

<?php 
   $map = new \Ds\Map(["1" => "Tutorials", "2" => "Point", "3" => "India"]);
   echo "The map elements are: \n";
   print_r($map);
   echo "The map elements are applying callback function: \n";
   $map->apply(function($key, $value){  
      return strtoupper($value);
   });
   print_r($map);
?>

Output

After executing the above program, the following output will be displayed −

The map elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 1
            [value] => Tutorials
        )

    [1] => Ds\Pair Object
        (
            [key] => 2
            [value] => Point
        )

    [2] => Ds\Pair Object
        (
            [key] => 3
            [value] => India
        )

)
The map elements are applying callback function:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 1
            [value] => TUTORIALS
        )

    [1] => Ds\Pair Object
        (
            [key] => 2
            [value] => POINT
        )

    [2] => Ds\Pair Object
        (
            [key] => 3
            [value] => INDIA
        )

)
php_function_reference.htm
Advertisements