在Python中,我可以使用"get"方法从字典中获取值而不会出现错误。
a = {1: "a", 2: "b"}
a[3] # error
a.get(3, "") # I got empty string.
所以我在寻找一个通用/基础函数来执行此操作:
function GetItem($Arr, $Key, $Default){
$res = '';
if (array_key_exists($Key, $Arr)) {
$res = $Arr[$Key];
} else {
$res = $Default;
}
return $res;
}
PHP和Python中基本上有相同的函数吗?
谢谢: dd