list
<?php
// 定义数组
$array = ['a', 'b', 'c'];
// 不使用 list()
$a = $array[0];
$b = $array[1];
$c = $array[2];
// 使用 list() 函数
list($a, $b, $c) = $array;
// explode
$string = 'hello|wild|world';
list($hello, , $world) = explode('|', $string);
echo $hello, ' ', $world;
// foreach
$arrays = [[1, 2], [3, 4], [5, 6]];
foreach ($arrays as list($a, $b)) {
$c = $a + $b;
echo $c, ', ';
}
extract
<?php
$array = [
'clothes' => 't-shirt',
'size' => 'medium',
'color' => 'blue',
];
extract($array);
echo $clothes, ' ', $size, ' ', $color;
compact
<?php
$clothes = 't-shirt';
$size = 'medium';
$color = 'blue';
$array = compact('clothes', 'size', 'color');
print_r($array);
// Array
// (
// [clothes] => t-shirt
// [size] => medium
// [color] => blue
// )