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
$map = new \Ds\Map([ "1" => "Geeks" ,
"2" => "for" , "3" => "Geeks" ]);
$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
$map = new \Ds\Map([ "1" => 5,
"2" => 10, "3" => 15]);
$callback = function ( $key , $value ){
return $value *10;
};
$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
)
)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
20 Aug, 2019
Like Article
Save Article