In Python I can use “get” method to get value from an dictionary without error.
a = {1: "a", 2: "b"}
a[3] # error
a.get(3, "") # I got empty string.
So I search for a common/base function that do this:
function GetItem($Arr, $Key, $Default){
$res = '';
if (array_key_exists($Key, $Arr)) {
$res = $Arr[$Key];
} else {
$res = $Default;
}
return $res;
}
Have same function basicly in PHP as in Python?
Thanks:
dd
isset()is typically faster thanarray_key_exists(). The parameter$defaultis initialized to an empty string if omitted.However, if an array key exists but has a NULL value,
isset()won’t behave the way you expect, as it will treat theNULLas though it doesn’t exist and return$default. If you expectNULLs in the array, you must usearray_key_exists()instead.