过去,php解构赋值,是使用list。从PHP 7.1开始增加了新的解构赋值语法。可以用[]进行解构赋值,并且list功能也同步增强。
比如:
$hobbies = ["cricket", "carrom", "chess"];
// 使用list函数。
list($cricket, $carrom, $chess) = $hobbies;
echo "I Love to play $cricket, $carrom and $chess";
// I Love to play cricket, carrom and chess
// 使用 []
[$cricket, $carrom, $chess] = $hobbies;
echo "I Love to play $cricket, $carrom and $chess";
// I Love to play cricket, carrom and chess
二者结果是一样的。 如果某些值不需要:
$hobbies = ["cricket", "carrom", "chess"];
// 使用list函数。
list($cricket, , $chess) = $hobbies;
echo "I Love to play $cricket and $chess";
//I Love to play cricket and chess
// 使用 []
[$cricket, , $chess] = $hobbies;
echo "I Love to play $cricket and $chess";
//I Love to play cricket and chess
交换变量:
// 使用list函数。
$a = 10;
$b = 20;
list($a, $b) = [$b, $a];
echo $a."\n"; // 20
echo $b."\n"; //10
// 使用 []
$a = 30;
$b = 40;
[$a, $b] = [$b, $a];
echo $a."\n"; //40
echo $b."\n"; //30
KeyValue型关联数组: 解包所有数值:
$student = ["roll_num" => 001, "name" => "John", "class" => "1st"];
// 使用list函数。
list("roll_num" => $roll_number, "name" => $name, "class" => $class) = $student;
echo $roll_number."\n"; // 001
echo $name."\n"; // John
echo $class."\n"; // 1st
// 使用 []
$student = ["roll_num" => 002, "name" => "Sam", "class" => "2nd"];
["roll_num" => $roll_number, "name" => $name, "class" => $class] = $student;
echo $roll_number."\n"; // 002
echo $name."\n"; // Sam
echo $class."\n"; // 2nd
使用键名对应写入目标
["class" => $class, "roll_num" => $roll_number, "name" => $name] = $student;
echo $roll_number; // 002
echo $name; // Sam
echo $class; // 2nd
跳过一些数值
// 使用list函数。
$numbers = ["a" => 10, "b" => 20, "c" => 30, "d" => 40];
list("a" => $a, "d" => $d) = $numbers;
echo $a."\n"; // 10
echo $d."\n"; // 40
// 使用 []
$numbers = ["a" => 10, "b" => 20, "c" => 30, "d" => 40];
["a" => $a, "c" => $c] = $numbers;
echo $a."\n"; // 10
echo $c."\n"; // 30
在foeach循环中使用:
$students = [
["roll_num" => 001, "name" => "John", "class" => "1st"],
["roll_num" => 002, "name" => "Sam", "class" => "2nd"]
];
// 使用list函数。
foreach ($students as list("roll_num" => $roll_number, "name" => $name,"class" => $class)) {
echo $roll_number ."\n";
echo $name ."\n";
echo $class ."\n";
}
// 使用 []
foreach ($students as ["roll_num" => $roll_number, "name" => $name,"class" => $class]) {
echo $roll_number ."\n";
echo $name ."\n";
echo $class ."\n";
}
$array = [[1, 2], [3, 4], [5, 6]];
foreach($array as list($odd, $even)){
echo "$odd is odd; $even is even", PHP_EOL;
}
/**
The output:
===
1 is odd; 2 is even
3 is odd; 4 is even
5 is odd; 6 is even
*/
嵌套解构
$options = ['enabled' => true, 'compression' => ['algo' => 'gzip']];
[
'enabled' => $enabled,
'compression' => [
'algo' => $compressionAlgo
]
] = $options;
// $enabled is TRUE
// $compressionAlgo is "gzip"
$x = ['o' => [[1, 2, 3], ['what' => 'WHAT']]];
['o' => [[$one, $two, $three], ['what' => $what]]] = $x;
/**
// $one is 1
// $two is 2
// $three is 3
// $what is "WHAT"
*/
写入数组,可以改变顺序
$info = array('coffee', 'brown', 'caffeine');
list($a[0], $a[1], $a[2]) = $info;
var_dump($a);
/**
array(3) {
[0]=>
string(6) "coffee"
[1]=>
string(5) "brown"
[2]=>
string(8) "caffeine"
}
*/
对于字符串,解构不工作,但是,字符串变量可以当成数组。
list($bar) = "abcde";
var_dump($bar);
// output: NULL
$string = "abcde";
list($foo) = $string;
var_dump($foo);
// output: string(1) "a"
遇到空值的情况 这个方式较好,免去了用太多的isset或??运算符。
$test = array("a","b","c","d");
unset($test[1]);
list($a,$b,$c)=$test;
print "\$a='$a' \$b='$b' \$c='$c'<BR>";
/**
// results in:
$a='a' $b='' $c='c'
// not:
$a='a' $b='c' $c='d'
*/
list处理函数返回数组
function getPosition($x, $y, $z)
{
// ... some operations like $x++...
return [$x, $y, $z];
}
list($x, $y, $z) = getPosition($x ,$y, $z);
最后,list 和 [] 还可以解构对象的属性。
class foo{
public $a = 1;
public $b = 2;
}
[$a,$b] = new Foo();
/**
// $a is 1
// $b is 2
*/
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 模板不会安装或需要功能定制以及二次开发?
发表评论
还没有评论,快来抢沙发吧!