2011-04-17 166 views
55

我需要从一个函数返回多个值,因此我已经将它们添加到数组并返回数组。PHP函数返回数组

<? 

function data(){ 

$a = "abc"; 
$b = "def"; 
$c = "ghi"; 

return array($a, $b, $c); 
} 


?> 

如何我可以通过调用上述函数接收$a$b$c值是多少?

+2

像你访问任何阵列可以存取权限的值。我建议阅读http://php.net/manual/en/language.types.array.php。 – 2011-04-17 09:01:34

回答

42

可以做到这一点:

list($a, $b, $c) = data(); 

print "$a $b $c"; // "abc def ghi" 
+3

Docs:http://www.php.net/manual/en/function.list。php – 2011-04-17 09:00:34

+0

@GiacomoTecyaPigani list不是一个函数,它是一个语言结构,如[docs](http://php.net/manual/en/function.list.php)所述。 – Taylan 2014-11-11 08:31:42

13

数据函数返回一个阵列,这样就可以访问相同的方式函数的结果作为您通常访问数组的元素:

<?php 
... 
$result = data(); 

$a = $result[0]; 
$b = $result[1]; 
$c = $result[2]; 

或者你可以使用list()功能,@fredrik建议,做同样的事情在一条线上。

80

您可以数组键添加到您的返回值,然后使用这些键打印数组值,如下所示:

function data() { 
    $out['a'] = "abc"; 
    $out['b'] = "def"; 
    $out['c'] = "ghi"; 
    return $out; 
} 

$data = data(); 
echo $data['a']; 
echo $data['b']; 
echo $data['c']; 
2

这里是一个类似功能的最佳途径

function cart_stats($cart_id){ 

$sql = "select sum(price) sum_bids, count(*) total_bids from carts_bids where cart_id = '$cart_id'"; 
$rs = mysql_query($sql); 
$row = mysql_fetch_object($rs); 
$total_bids = $row->total_bids; 
$sum_bids = $row->sum_bids; 
$avarage = $sum_bids/$total_bids; 

$array["total_bids"] = "$total_bids"; 
$array["avarage"] = " $avarage"; 

return $array; 
} 

,你会得到这样的

$data = cart_stats($_GET['id']); 
<?=$data['total_bids']?> 
0

数组数据,我认为这样做是创建一个全球性的最佳途径var数组。然后通过传递它作为参考,在函数数据中做任何你想做的事情。不需要返回任何东西。

$array = array("white", "black", "yellow"); 
echo $array[0]; //this echo white 
data($array); 

function data(&$passArray){ //<<notice & 
    $passArray[0] = "orange"; 
} 
echo $array[0]; //this now echo orange 
1

这是我做的警予framewok内:

public function servicesQuery($section){ 
     $data = Yii::app()->db->createCommand() 
       ->select('*') 
       ->from('services') 
       ->where("section='$section'") 
       ->queryAll(); 
     return $data; 
    } 

那么我认为文件中:

 <?php $consultation = $this->servicesQuery("consultation"); ?> ?> 
     <?php foreach($consultation as $consul): ?> 
      <span class="text-1"><?php echo $consul['content']; ?></span> 
     <?php endforeach;?> 

我在做什么抓住了表我有一个白痴部分选择。应该只是PHP减去“Yii的”工作方式为DB

17
function give_array(){ 

    $a = "abc"; 
    $b = "def"; 
    $c = "ghi"; 

    return compact('a','b','c'); 
} 


$my_array = give_array(); 

http://php.net/manual/en/function.compact.php

+1

不要忘了,你也可以使用'extract($ my_array)'将数组分离回变量:http://php.net/manual/en/function.extract.php – 2013-05-23 17:56:23

+1

这很酷......但在我的经验中不是很常用。 – 2014-05-02 04:24:11

1

根本的问题围绕着访问数组内的数据,菲利克斯克林第一响应指出。

在下面的代码中,我用print和echo构造函数访问了数组的值。

function data() 
{ 

    $a = "abc"; 
    $b = "def"; 
    $c = "ghi"; 

    $array = array($a, $b, $c); 

    print_r($array);//outputs the key/value pair 

    echo "<br>"; 

    echo $array[0].$array[1].$array[2];//outputs a concatenation of the values 

} 

data(); 
4

从PHP 5.4,你可以利用数组语法,做这样的事情:

<? 

function data() 
{ 
    $retr_arr["a"] = "abc"; 
    $retr_arr["b"] = "def"; 
    $retr_arr["c"] = "ghi"; 

    return $retr_arr; 
} 

$a = data()["a"]; //$a = "abc" 
$b = data()["b"]; //$b = "def" 
$c = data()["c"]; //$c = "ghi" 
?> 
+7

注意:您可能不想评估该功能3次! – 2017-01-10 18:23:51

2
<?php 
function demo($val,$val1){ 
    return $arr=array("value"=>$val,"value1"=>$val1); 

} 
$arr_rec=demo(25,30); 
echo $arr_rec["value"]; 
echo $arr_rec["value1"]; 
?>