Let's say we have an array, and we try to get the value of a key which does not exist:
$arr = ['a', 'b'];
$val0 = $arr[0] ?? null;
$val1 = $arr[1] ?? null;
$val2 = $arr[2] ?? null;
Because of the null coalescing operator, this will not emit any notices, regardless of whether the array elements exist.
In PHP7.1 symmetric array destructuring was introduced; it is helpful, but this code:
[$val0, $val1, $val2] = $arr;
will emit an Undefined index: 2 in $arr ...
notice.
Is it possible to destructure the array while avoiding these notices?
$ourArray = array_merge(['a' => null, 'b' => null, 'c' => null], $ourArray);
– Corydenlist
here? – Dana$foo = array(1, 2, 3);
instead of$foo = [1, 2, 3];
the code works fine but his code is complaining that the c key is not defined (notice how the first one throws a notice?) – Corydena, b, c = ourArray.values_at('a', 'b', 'c')
, wherec
would be nil. Maybe similar api exists here. – Dana