PHP list 关键字
实例
将数组的元素分配给变量:
<?php
list($a, $b, $c) = [1, 2, 3];
echo "$a is " . $a . "<br>";
echo "$b is " . $b . "<br>";
echo "$c is " . $c . "<br>";
?>
亲自试一试 »
list($a, $b, $c) = [1, 2, 3];
echo "$a is " . $a . "<br>";
echo "$b is " . $b . "<br>";
echo "$c is " . $c . "<br>";
?>
定义和用法
list
关键字关键字将数组元素分配给变量列表。
如果数组中没有足够的元素,它会输出一个通知,并将空值赋给剩余的变量。
注释: 自 PHP 7.1.0 起,可以使用箭头 =>
语法选择分配给变量的元素。
更多实例
实例
选择将哪个元素分配给哪个变量:
<?php
list(2 => $a, 0 => $b, 1 => $c) = [1, 2, 3];
echo "$a is " . $a . "<br>";
echo "$b is " . $b . "<br>";
echo "$c is " . $c . "<br>";
?>
亲自试一试 »
list(2 => $a, 0 => $b, 1 => $c) = [1, 2, 3];
echo "$a is " . $a . "<br>";
echo "$b is " . $b . "<br>";
echo "$c is " . $c . "<br>";
?>