PHP - Ds Map::remove() Function



The PHP Ds\Map::remove() function is used to remove a value specified by the key, and it returns the value associated with that key. If the specified key is not found in the map, it returns the default value provided.

This function throws an OutOfBoundsException if a key is not found in the current map and a default value is not provided.

Syntax

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

public mixed Ds\Map::remove( mixed $key [, mixed $default ] )

Parameters

Following are the parameters of this function −

  • key − The key to remove.
  • default (optional) − An optional default value, returned if the key is not found.

Return value

This function returns a value that was removed, or a default value if provided when a key can't be found in a map.

Example 1

The following is the basic example of the Ds\Map::remove() function −

<php  
   $map = new \Ds\Map([1, 2, 3, 4, 5]);
   echo("The map elements are: \n");  
   print_r($map);
   $k = 3;
   echo "The key value is: ".$k;
   echo("\nThe removed element of a map: ");  
   var_dump($map->remove($k));  
?>

Output

The above program displays the following output −

The map elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => 1
        )

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

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

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

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

)
The key value is: 3
The removed element of a map: int(4)

Example 2

Following is another example of the PHP Ds\Map::remove() function. We use this function to remove a value specified by the key "c" of this map (["a"=>"Tutorials", "b"=>"Point", "c"=>"India", "d"=>"Tutorix"]) −

<?php  
   $map = new \Ds\Map(["a"=>"Tutorials", "b"=>"Point", "c"=>"India", "d"=>"Tutorix"]);  
   echo("The map elements are: \n");  
   print_r($map);
   $k = "c";
   echo "The key value is: ";
   echo("\nThe removed element of a map: ");  
   var_dump($map->remove($k));  
?>

Output

After executing the above program, it will produce the following output −

The map elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => Tutorials
        )

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

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

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

)
The key value is:
The removed element of a map: string(5) "India"

Example 3

If the specified key is not found in the map, this function throws "OutOfBoundsException" −

<?php  
   $map = new \Ds\Map([10, 20, 30]);  
   echo("The map elements are: \n");  
   print_r($map);
   $k = 4;
   echo "The key value is: ";
   echo("\nThe removed element of a map: ");  
   var_dump($map->remove($k));  
?>

Output

The above program throws the following exception −

The map elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => 10
        )

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

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

)
The key value is:
The removed element of a map: PHP Fatal error:  
Uncaught OutOfBoundsException: Key not found in C:\Apache24\htdocs\index.php:8
Stack trace:
#0 C:\Apache24\htdocs\index.php(8): Ds\Map->remove(4)
#1 {main}
  thrown in C:\Apache24\htdocs\index.php on line 8

Example 4

If the specified key is not found in the map, this function returns the provided "default" value −

<?php  
   $map = new \Ds\Map(["1"=> 'a', "2"=>'e', "3"=>'i']);  
   echo("The map elements are: \n");  
   print_r($map);
   $k = "4";
   $d = "o";
   echo "The key value is: ".$k;
   echo "\nThe default value is: ".$d;
   echo ("\nIf the key does not found (default value): ");  
   var_dump($map->remove($k, $d));
?>

Output

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

The map elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 1
            [value] => a
        )

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

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

)
The key value is: 4
The default value is: o
If the key does not found (defualt value): string(1) "o"
php_function_reference.htm
Advertisements