Avoid PHP notice when destructuring array
Solution with @
You can use the @
operator.
https://secure.php.net/manual/en/language.operators.errorcontrol.php
@[ 'c' => $someValue] = $ourArray;
Disclaimer
This operator is controversial. It may hide useful errors messages from function calls. A lot of programmers will avoid it even for hight cost. For assignments it is safe though.
Solution with defaults
Based on comment by h2ooooooo.
If You can and want define all defaults, You can use code below.
[ 'c' => $someValue] = $ourArray + $defaults;
The operator +
is important. The function array_merge
will not preserve numeric keys.
The definition for $defaults
may look like this. You have to define values for every possible key.
$defaults = [ 'a' => null, 'b' => null, 'c' => null, 'd' => null, 'e' => null, 'f' => null,];# or$defaults = array_fill_keys( ['a', 'b', 'c', 'd', 'e', 'f'], null);