Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
php maps
communitycreator

What is the Ds\Map::apply() function in PHP?

Khizar Hayat Saani

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Map class in PHP is a collection of key-value pairs. The structure of a Map is similar to an array in terms of the sequence (insertion order is preserved), performance, and memory efficiency.

The apply() function is used to apply a callback function on each value within the Map.

The Ds\map::apply() function

Syntax

The following is the function prototype:

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

Parameters and return value

The apply() function takes the following input parameter:

$callback: The callback function to be applied on each value.

Note: The callback function must return the appropriate data type for the values in the map.

Since the function has a return type of void, nothing is returned. The original map is updated to contain new values based on the values returned by the callback function.

Code

<?php

// Creating a Map
$map = new \Ds\Map(["0" => "a", 
            "1" => "b", "2" => "c", "3" => "d", "4" => "e", "5" => "f"]);
            
// Print original map
echo "Original Map: \n";
print_r($map);
  
echo"--------------------------------------\n";

$map->apply(function($key, $value){
                    return strtoupper($value);});
                    
// Print new map
echo "New Map: \n";
print_r($map);

?>

Output

Original Map:
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => 0
[value] => a
)

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

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

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

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

[5] => Ds\Pair Object
(
[key] => 5
[value] => f
)

)
--------------------------------------
New Map:
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => 0
[value] => A
)

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

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

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

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

[5] => Ds\Pair Object
(
[key] => 5
[value] => F
)

)

Explanation

In the example above, the callback function merely takes the value and changes it to uppercase.

The original map is first printed. Then the apply() function is called. Printing the map now shows that all the values have been changed to uppercase.

Note: To run this program, you must first install Data Structures for PHP on your local machine.

RELATED TAGS

php
php maps
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring