Is it possible to pass in an array to a function and return that same array object back out?
function array_setdefault($arr, $key, $dflt) {
if (array_key_exists($key, $arr)) {
return $arr[$key];
}
$arr[$key] = $dflt;
return $arr[$key];
}
$errors = ["foo" => ["bar"]];
array_push(array_setdefault($errors, "foo", []), "bim");
array_push(array_setdefault($errors, "bla", []), "wub");
var_dump($errors);
leads to:
array(1) {
["foo"]=>
array(1) {
[0]=>
string(3) "bar"
}
}
but I'd like to have:
array(2) {
["foo"]=>
array(2) {
[0]=>
string(3) "bar"
[1]=>
string(3) "bim"
}
["bla"]=>
array(1) {
[0]=>
string(3) "wub"
}
}
I.e. $dflt should be set on the array for the $key in case it's not been set. If it has been set previously or after it's set in the current call to array_setdefault, return what's at $arr[$key] (that is, the $dflt array object).