我需要从一个函数返回多个值,因此我将它们添加到一个数组中并返回该数组。
<?
function data(){
$a = "abc";
$b = "def";
$c = "ghi";
return array($a, $b, $c);
}
?>
如何通过调用上述函数获取$a
、$b
、$c
的值?
发布于 2011-05-27 23:49:29
您可以将数组键添加到返回值中,然后使用这些键来打印数组值,如下所示:
function data() {
$out['a'] = "abc";
$out['b'] = "def";
$out['c'] = "ghi";
return $out;
}
$data = data();
echo $data['a'];
echo $data['b'];
echo $data['c'];
发布于 2011-04-17 16:56:47
您可以这样做:
list($a, $b, $c) = data();
print "$a $b $c"; // "abc def ghi"
发布于 2012-11-10 03:29:58
function give_array(){
$a = "abc";
$b = "def";
$c = "ghi";
return compact('a','b','c');
}
$my_array = give_array();
https://stackoverflow.com/questions/5692568
复制相似问题