php中的list方法

list

栗子一:

<?php
$info = array('coffee', 'brown', 'caffeine');

// 列出所有变量
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.
";

// 列出他们的其中一个
list($drink, , $power) = $info;
echo "$drink has $power.
";

// 或者让我们跳到仅第三个
list( , , $power) = $info;
echo "I need $power!
";

// list() 不能对字符串起作用
list($bar) = "abcde";
var_dump($bar); // NULL
?>
coffee is brown and caffeine makes it special.
coffee has caffeine.
I need caffeine!
NULL

栗子二:

<?php

list($a, list($b, $c)) = array(1, array(2, 3));

var_dump($a, $b, $c);

?>
int(1)
int(2)
int(3)

栗子三:

<?php

$info = array('coffee', 'brown', 'caffeine');

list($a[0], $a[1], $a[2]) = $info;

var_dump($a);

?>
array(3) {
  [2]=>
  string(8) "caffeine"
  [1]=>
  string(5) "brown"
  [0]=>
  string(6) "coffee"
}

比较冷门,可以尝试着使用一下。

原文地址:https://www.cnblogs.com/jiqing9006/p/9089882.html