Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of PHP7.1 Square bracket Array symbol Multi-value copy and specified key assignment

Shulou Source: shulou.com Published: 2022-06-03 13:02:03 10月04日 Update

This article introduces the example analysis of PHP7.1 square bracket array symbol multi-valued replication and specified key assignment usage, the content is very detailed, interested friends can refer to, hope to be helpful to you.

PHPer may all know the use of list, which simply means that you can assign values to multiple variables through an array in an expression test:

$values = array ('value1',' value2'); $list ($v1, $v2) = $values

Doesn't it feel convenient? In PHP 7.1, it is also easier to:

[$v1, $v2] = ['foo',' bar']

This is not the best part. In PHP 7.1, we can also specify key values to assign values, regardless of the order of the array elements:

List ('v1' = > $value1,' v2' = > $value2) = array ('v1' = >' foo', 'v2' = >' bar',...); / / or ['v1' = > $value1,' v2' = > $value2] = ['v1' = >' foo', 'v2' = >' bar',...]

In fact, in the era of PHP 5, there is a very good use of list may not be familiar with:

$arr = [['x','y'], ['x','y2'],]; foreach ($arr as list ($x, $y)) {echo $x,'', $y, PHP_EOL;}

By PHP 7.1, because you can specify a key assignment, this usage will be more flexible and probably more common:

$arr = [['x' = > 1,'y' = >'2'], ['x' = > 2,'y' = >'4'],]; foreach ($arr as ['x' = > $x,'y' = > $y) {echo $x,'', $y, PHP_EOL;}

Let's take a look at an example on the official website to see if it feels as refreshing as the spring breeze:

Class ElePHPant {private $name, $colour, $age, $cuteness; public function _ construct (array $attributes) {/ / $this- > name = $attributes ['name']; / / before / / now ["name" = > $this- > name, "colour" = > $this- > colour, "age" = > $this- > age, "cuteness" = > $this- > cuteness] = $attributes;} / /.}

It is worth mentioning that this way of assignment can be used in nesting!

[[$a, $b], [$c, $d]] = [[1,2], [3,4]]

Finally, there is a vision in the proposal of PHP 7.1, which is also very much to look forward to:

Class ElePHPant {private $name, $colour, $age, $cuteness; public function _ construct (["name" = > string $name, "colour" = >\ Colour $colour, "age" = > int $age, "cuteness" = > float $cuteness]) {$this- > name = $name; $this- > colour = $colour; $this- > age = $age; $this- > cuteness = $cuteness;} / /.}

If PHP introduces this syntax, the parameter list will no longer care about the parameter order, and PHP's buddies will no longer envy Ruby's buddies!

On the PHP 7.1 square bracket array symbol multi-value copy and specify key assignment usage example analysis is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Arrays guys buddies square brackets examples symbols analysis good content parameters feeling more order help refreshing worth mentioning no things examples elements Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MySQL vpn Apple macOS