22

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?

9
  • 2
    If you know what keys the array should have why not merge them before doing anything else? $ourArray = array_merge(['a' => null, 'b' => null, 'c' => null], $ourArray);
    – h2ooooooo
    Feb 16, 2018 at 12:23
  • 4
    Your example of array destructing doesn't make sense...
    – Jonnix
    Feb 16, 2018 at 12:25
  • @JonStirling could you tell me please why it doesnt make any sense?
    – Manuel
    Feb 16, 2018 at 12:26
  • The documentation doesn't mention this style of destructuring. Why do you think it should be possible like this, using dictionary "flavor" of the arrays on the receiving side? Feb 16, 2018 at 12:28
  • 1
    @SergioTulentsev Using the error suppression operator that Michas mentioned is probably the easiest way without doing merging of the array or filling with NULL. I wasn't aware of this new syntax either. Personally I'd probably go for a different solution all together.
    – h2ooooooo
    Feb 16, 2018 at 12:40

2 Answers 2

37

You can use the @ operator.
https://secure.php.net/manual/en/language.operators.errorcontrol.php

@[$val0, $val1, $val2] = $arr;

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.


Based on comment by h2ooooooo.

If you are able to define defaults for all your values, you can use code below.

[$val0, $val1, $val2] = $arr + $defaults;

The operator + is important. The function array_merge will not preserve numeric keys.

The definition for $defaults may look like this:

$defaults = array_fill(0, 3, null);

Or you can manually define values for every possible key.

6
  • No clue who downvoted you (probably just for hiding errors in general). This answer works.
    – h2ooooooo
    Feb 16, 2018 at 12:34
  • 7
    Many developers discourage the "stfu operator". Yes, it works, but it generally indicates a hacky solution that should be replaced by something more appropriate (I didn't downvote). Feb 16, 2018 at 12:56
  • I would also accept @h2ooooooo solution: $ourArray = array_merge(['a' => null, 'b' => null, 'c' => null], $ourArray);
    – Manuel
    Feb 16, 2018 at 13:02
  • 2
    You can simplify the $defaults definition by doing something like $defaults = array_fill_keys(['a', 'b', 'c', 'd', 'e', 'f'], null);
    – h2ooooooo
    Feb 16, 2018 at 19:10
  • Neither of these solutions work on a foreach statement, unfortunately. Dec 1, 2020 at 1:03
10

You could try ensuring the key exists in the array, by merging some defaults:

[$val0, $val1, $val2] = $arr + [2 => null];
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.