Ds\Map::remove
(PECL ds >= 1.0.0)
Ds\Map::remove — 通过键删除并返回一个值。
描述
public mixed Ds\Map::remove ( mixed $key [, mixed $default ] )
按键移除并返回一个值,如果找不到键则返回一个可选的默认值。
Note: Keys of type object are supported. If an object implements Ds\Hashable, equality will be determined by the object's
equals
function. If an object does not implement Ds\Hashable, objects must be references to the same instance to be considered equal.
注意:您也可以使用数组语法通过键访问值,例如。
$map["key"]
。
警告
使用数组语法时要小心。标量键将被引擎强制为整数。例如,$map["1"]
会尝试访问int(1)
,同时$map->get("1")
会正确查找字符串键。
见数组。
参数
key
要删除的关键。
default
可选的默认值,如果找不到密钥则返回。
返回值
已删除的default
值,或者提供的值以及key
在地图中找不到的值。
错误/异常
OutOfBoundsException如果找不到密钥并且未提供默认值。
例子
示例#1 Ds\Map::remove() 示例
<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
var_dump($map->remove("a")); // 1
var_dump($map->remove("e", 10)); // 10 (default used)
?>
上面的例子会输出类似于:
int(1)
int(10)
← Ds\Map::reduce
Ds\Map::reverse →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com