我有一大块代码,比如:
<?php
$person = array("John", "Smith", "Male", "Green");
$firstN = $person[0];
$lastN = $person[1];
$gender = $person[2];
$favColor = $person[3];
?>
我曾经看到一些代码整合了这一点,但我不知道它是如何完成的,也找不到我看到这个例子的页面。它有点像:
<?php
$person = array("John", "Smith", "Male", "Green");
someFunction($firstN, $lastN, $gender,$favColor) = $person;
?>
它根据数组中的值分配变量,方法与第一个例子相同
在上面的例子中,someFunction
可能是什么?
您可能正在寻找PHP的list()函数。
例如:
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
在您的情况下:
$person = array("John", "Smith", "Male", "Green");
list($firstN, $lastN, $gender,$favColor) = $person;
为什么是!实际上有一种很酷的方法:
list($firstN, $lastN, $gender, $favColor) = $person;
就这样!所有变量都将被填充。
请参阅list()
PHP手动输入。