The Ds\Map::apply() function of the Map class in PHP is used to apply a specific operation to all of the elements present in the map. It accepts a callback function and updates all of the elements present in the Map according to the given callback function.
Syntax:
void public Ds\Map::apply ( callable $callback )
Note: The callback function should return the updated value for specific key-value pair.
Parameters: It accepts a callback function as a parameter and updates all of the elements according to that function.
Return value: This function does not returns any value.
Below programs illustrate the Ds\Map::apply() function in PHP:
Program 1:
<?php // PHP program to illustrate the apply() // function of Ds\map // Creating a Map $map = new \Ds\Map(["1" => "Geeks", "2" => "for", "3" => "Geeks"]); // Converting all elements to uppercase // using callback function $map->apply(function($key, $value){ return strtoupper($value); }); print_r($map); ?> |
Output:
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => 1
[value] => GEEKS
)
[1] => Ds\Pair Object
(
[key] => 2
[value] => FOR
)
[2] => Ds\Pair Object
(
[key] => 3
[value] => GEEKS
)
)
Program 2:
<?php // PHP program to illustrate the apply() // function of Ds\map // Creating a Map $map = new \Ds\Map(["1" => 5, "2" => 10, "3" => 15]); // Declare the callback function $callback = function($key, $value){ return $value*10; }; // Multiplying each value by 10 // using callback function $map->apply($callback); print_r($map); ?> |
Output:
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => 1
[value] => 50
)
[1] => Ds\Pair Object
(
[key] => 2
[value] => 100
)
[2] => Ds\Pair Object
(
[key] => 3
[value] => 150
)
)
Recommended Posts:
- PHP | Ds\Vector apply() Function
- PHP | Ds\Deque apply() Function
- PHP | Ds\Sequence apply() Function
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

