Php 简明教程
PHP - Array Destructuring
在 PHP 中,数组解构一词指的是将数组元素提取到单个变量中的机制。它也可以称为解包数组。PHP 的 list() 构造函数用来解构给定的数组,并在一条语句中将其项目分配给变量列表。
In PHP, the term Array destructuring refers to the mechanism of extracting the array elements into individual variables. It can also be called unpacking of array. PHP’s list() construct is used to destrucrure the given array assign its items to a list of variables in one statement.
list($var1, $var2, $var3, . . . ) = array(val1, val2, val3, . . .);
结果, val1 被分配到 $var1, val2 到 $var2 等等。即使因为括号,您可能认为 list() 是一个函数,但它不是,因为它没有返回值。PHP 将字符串视为一个数组,但是它无法用 list() 解包。此外,list() 中的括号不能是空的。
As a result, val1 is assigned to $var1, val2 to $var2 and so on. Even though because of the parentheses, you may think list() is a function, but it’s not as it doesn’t have return value. PHP treats a string as an array, however it cannot be unpacked with list(). Moreover, the parenthesis in list() cannot be empty.
除了 list(),您还可以使用方括号 [] 作为解构数组的快捷方式。
Instead of list(), you can also use the square brackets [] as a shortcut for destructuring the array.
[$var1, $var2, $var3, . . . ] = array(val1, val2, val3, . . .);
Example
请看以下示例:
Take a look at the following example −
<?php
$marks = array(50, 56, 70);
list($p, $c, $m) = $marks;
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
# shortcut notation
[$p, $c, $m] = $marks;
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Physics: 50 Chemistry: 56 Maths: 70
Physics: 50 Chemistry: 56 Maths: 70
Destructuring an Associative Array
在 PHP 7.1.0 之前,list() 只在从 0 开始的数字索引的数字数组上工作。PHP 7.1 数组解构也适用于关联数组。
Before PHP 7.1.0, list() only worked on numerical arrays with numerical indices start at 0. PHP 7.1, array destructuring works with associative arrays as well.
让我们尝试对以下关联数组进行解构(或拆包),一个具有非数字索引的数组。
Let us try to destructure (or unpack) the following associative array, an array with non-numeric indices.
$marks = array('p'=>50, 'c'=>56, 'm'=>70);
要对这个数组进行解构,list() 语句将每个数组键与一个独立变量相关联。
To destructure this array the list() statement associates each array key with a independent variable.
list('p'=>$p, 'c'=>$c, 'm'=>$m) = $marks;
此外,您还可以使用 [] 替代解构符号。
Instead, you can also use the [] alternative destructuring notation.
['p'=>$p, 'c'=>$c, 'm'=>$m] = $marks;
尝试并执行以下 PHP 脚本 −
Try and execute the following PHP script −
<?php
$marks = array('p'=>50, 'c'=>56, 'm'=>70);
list('p'=>$p, 'c'=>$c, 'm'=>$m) = $marks;
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
# shortcut notation
['p'=>$p, 'c'=>$c, 'm'=>$m] = $marks;
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
?>
Skipping Array Elements
对于索引数组,您可以跳过其某些元素,只将其他元素分配给必需的变量
In case of an indexed array, you can skip some of its elements in assign only others to the required variables
<?php
$marks = array(50, 56, 70);
list($p, , $m) = $marks;
echo "Physics: $p Maths: $m" . PHP_EOL;
# shortcut notation
[$p, , $m] = $marks;
echo "Physics: $p Maths: $m" . PHP_EOL;
?>
对于关联数组,由于索引不是从 0 开始递增的,因此在分配时不必遵循元素的顺序。
In case of an associative array, since the indices are not incremental starting from 0, it is not necessary to follow the order of elements while assigning.
<?php
$marks = array('p'=>50, 'c'=>56, 'm'=>70);
list('c'=>$c, 'p'=>$p, 'm'=>$m) = $marks;
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
['c'=>$c, 'm'=>$m, 'p'=>$p] = $marks; # shortcut notation
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
?>
Destructuring a Nested Array
您还可以将数组解构的概念扩展到嵌套数组。在以下示例中,嵌套在其内部的子数组是一个索引数组。
You can extend the concept of array destructuring to nested arrays as well. In the following example, the subarray nested inside is an indexed array.
<?php
$marks = ['marks' => [50, 60, 70]];
['marks' => [$p, $c, $m]] = $marks;
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
?>
即使嵌套数组也是关联数组,解构也同样有效。
Destructuring works well even if the nested array is also an associative array.
<?php
$marks = ['marks' => ['p'=>50, 'c'=>60, 'm'=>70]];
['marks' => ['p'=>$p, 'c'=>$c, 'm'=>$m]] = $marks;
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
?>