PHP | Ds\Map remove()函数
Ds\Map::remove()函数是PHP中的一个内置函数,用于通过键删除和返回值。
句法:
mixed Ds\Map::remove( $key, $default )
参数:该函数接受上面提到的两个参数,如下所述:
- $key:保存需要删除的键值。
- $default:它是可选参数,如果没有找到键则返回。
返回值:此函数返回已删除的值。
异常:如果键不存在且未提供 $default 的值,此函数将抛出 OutOfRangeException。
下面的程序说明了PHP中的 Ds\Map::remove()函数:
示例 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"
例子:
"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"
参考: https://www. PHP.net/manual/en/ds-map.remove。 PHP