Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Map::put() Function



The PHP Ds\Map::put() function is used to associate a key with a value. Associating a key with a value involves mapping or linking a specific key to a particular value.

If an association already exists for that key, the existing value is replaced with the new value.

Syntax

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

public void Ds\Map::put( mixed $key , mixed $value )

Parameters

This function accepts two parameters named 'key', and 'value', which are described below −

  • key − The key to associate the value with.
  • value − The value needs to be associated with the key.

Return value

This function doesn't return any value.

Example 1

The following program demonstrates the usage of the PHP Ds\Map::put() function −

<?php  
   $map = new \Ds\Map();
   echo "The map elements before using put() function: \n";
   print_r($map);
   $k1 = 1;
   $val1 = 10;
   $k2 = 2; 
   $val2 = 20;
   echo "The key1 and key2 are: ".$k1.", ".$k2;
   echo "\nThe values are: ".$val1.", ".$val2;
   #using put() function
   $map->put("1", "10");
   $map->put("2", "20");
   echo "\nThe map elements after using put() function: \n";
   print_r($map);
?>  

Output

The above program displays the following output −

The map elements before using put() function:
Ds\Map Object
(
)
The key1 and key2 are: 1, 2
The values are: 10, 20
The map elements after using put() function:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 1
            [value] => 10
        )

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

)

Example 2

Following is another example of the PHP Ds\Map::put() function. We use this function to associate a key "a" with a value "Tutorials" −

<?php  
   $map = new \Ds\Map(["b"=>"Point", "c"=>"India"]);
   echo "The map elements before using put() function: \n";
   print_r($map);
   $k = "a";
   $val = "Tutorials";
   echo "The key and value are: ".$k.", ".$val;
   #using put() function
   $map->put($k, $val);
   echo "\nThe map elements after using put() function: \n";
   print_r($map);
?>

Output

Once the above program is executed, it will display the following output −

The map elements before using put() function:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => b
            [value] => Point
        )

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

)
The key and value are: a, Tutorials
The map elements after using put() function:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => b
            [value] => Point
        )

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

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

)
php_function_reference.htm
Advertisements