PHP | Ds\Map remove() Function
The Ds\Map::remove() function is an inbuilt function in PHP which is used to remove and return a value by key.
Syntax:
mixed Ds\Map::remove( $key, $default )
Parameters: This function accepts two parameters as mentioned above and described below:
- $key: It holds the key value which need to remove.
- $default: It is optional parameter and it returns if key not found.
Return Value: This function returns the value that was removed.
Exception: This function throws OutOfRangeException if the key not exist and value of $default is not provided.
Below programs illustrate the Ds\Map::remove() function in PHP:
Example 1:
<?php // Declare new Map $map = new \Ds\Map([ 1 => "geeks", 2 => "for", 3 => "geeks", 4 => "DataStructures"]); echo("Map Elements\n"); // Display the map elements print_r($map); echo("\nRemoved element of the map: \n"); // Use remove() function to remove // value at index 3 and return it var_dump($map->remove(3)); ?> |
Map Elements
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
)
[3] => Ds\Pair Object
(
[key] => 4
[value] => DataStructures
)
)
Removed element of the map:
string(5) "geeks"
Example:
<?php // Declare new Map $map = new \Ds\Map([ "a" => "geeks", "b" => "for", "c" => "geeks", "d" => "DataStructures"]); echo("Map Elements\n"); // Display the map elements print_r($map); echo("\nRemoved element of the map: \n"); // Use remove() function to remove // value at index 3 and return it var_dump($map->remove("c")); ?> |
Map Elements
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => a
[value] => geeks
)
[1] => Ds\Pair Object
(
[key] => b
[value] => for
)
[2] => Ds\Pair Object
(
[key] => c
[value] => geeks
)
[3] => Ds\Pair Object
(
[key] => d
[value] => DataStructures
)
)
Removed element of the map:
string(5) "geeks"
Reference: https://www.php.net/manual/en/ds-map.remove.php
Recommended Posts:
- PHP Ds\Set remove() Function
- D3.js | d3.map.remove() Function
- p5.js | remove() Function
- D3.js | d3.set.remove() function
- PHP | Ds\Vector remove() Function
- PHP | Ds\Deque remove() Function
- PHP | Ds\Sequence remove() Function
- PHP | Ds\Vector remove() Function
- How to remove style added with .css() function using JavaScript?
- How to remove error Call to undefined function curl_init()?
- Remove a cookie using PHP
- How to remove a key and its value from an associative array in PHP ?
- How to remove CSS property using JavaScript?
- Remove new lines from string in PHP
- How to remove the first character of string in PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



