10

I mean something like this:

$arr = ["1", "2", "3"];

$one;
$two;
$three;

$new = [$one, $two, $three] = $arr; 

As result I should get:

$one = 1;
$two = 2;
$three = 3;

I tried, it works in PHP 7.3.2:

var_dump($one); // returns 1
6
  • 3
    If you try it that is what you'll get FWIW :) Commented Feb 26, 2019 at 20:09
  • 3
    kinda.. but using extract() or list() functons Commented Feb 26, 2019 at 20:10
  • 1
    So it seems, 3v4l.org/qGTAE >= php 7.1.0 Commented Feb 26, 2019 at 20:12
  • 1
    @LawrenceCherone: 3v4l.org/BAchH Commented Feb 26, 2019 at 20:13
  • 1
    I was already going to ask myself why I simulated the test and it worked, taking advantage, what would be its use? Commented Feb 26, 2019 at 20:15

1 Answer 1

7

Indeed, it is listed as a new feature in PHP 7.1:

The shorthand array syntax ([]) may now be used to destructure arrays for assignments (including within foreach), as an alternative to the existing list() syntax, which is still supported.

Sign up to request clarification or add additional context in comments.

1 Comment

The new shorthand also supports destructuring arrays with string keys by key rather than by order, a very nice addition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.