Wednesday, May 27, 2026
HomeLanguagesPHP | DsMap map() Function

PHP | Ds\Map map() Function

The Ds\Map::map() function of the Map class in PHP is used to apply a callback function to a Map object. This returns the result of applying the callback function to each value present on the map. The function does not update the values in the original map, instead, it just returns the result of the updates without affecting the original values.

Syntax:

Ds\Map public Ds\Map::map ( callable $callback )

Parameter: It accepts a callback function as a parameter. This callback function applies a specific operation on each value of the map.

Return value: This function returns the result of applying the callback function on each value of the map without affecting the original values.

Below program illustrate the Ds\Map::map() function in PHP:
Program:




<?php
// PHP program to illustrate the map()
// function of Ds\map
  
// Creating a Map
$map = new \Ds\Map(["1" => "Geeks", 
            "2" => "for", "3" => "Geeks"]);
  
// Print the result of map() function
print_r($map->map(function($key, $value){
                    return strtoupper($value); 
}));
  
// Print the actual map
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
        )

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

)

Reference: http://php.net/manual/en/ds-map.map.php

RELATED ARTICLES

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS