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
This language construct is used to "pull" variables out of an array. In other words: it will "destructure" the array into separate variables.
Object Destructuring (PHP 7.1)Unfortunately there is no object destructuring. However you can convert an object to an associative array using get_object_vars , and then use associative array destructuring.
Destructuring in Arrays. To destructure an array in JavaScript, we use the square brackets [] to store the variable name which will be assigned to the name of the array storing the element. const [var1, var2, ...]
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 withinforeach
), as an alternative to the existinglist()
syntax, which is still supported.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With