PHP 函数返回数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5692568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:14:20  来源:igfitidea点击:

PHP function return array

phparraysfunction

提问by Ajay

I need to return multiple values from a function, therefore I have added them to an array and returned the array.

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

<?

function data(){

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

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


?>

How can I receive the values of $a, $b, $cby calling the above function?

我怎么能接受的价值观$a$b$c通过调用上述功能?

回答by Kristoffer Bohmann

You can add array keys to your return values and then use these keys to print the array values, as shown here:

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

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

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

回答by fredrik

you can do this:

你可以这样做:

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

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

回答by ITS Alaska

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

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

回答by Nick

The data function is returning an array, so you can access the result of the function in the same way as you would normally access elements of an array:

data 函数返回一个数组,因此您可以像通常访问数组元素一样访问该函数的结果:

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

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

Or you could use the list()function, as @fredrik recommends, to do the same thing in a line.

或者,您可以list()按照@fredrik 的建议使用该函数在一行中执行相同的操作。

回答by Headshota

$array  = data();

print_r($array);

回答by Obinwanne Hill

From PHP 5.4 you can take advantage of array dereferencing and do something like this:

从 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"
?>

回答by mohd jagir

<?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"];
?>

回答by user3589574

In order to get the values of each variable, you need to treat the function as you would an array:

为了获得每个变量的值,您需要像对待数组一样对待函数:

function data() {
    $a = "abc";
    $b = "def";
    $c = "ghi";
    return array($a, $b, $c);
}

// Assign a variable to the array; 
// I selected $dataArray (could be any name).

$dataArray = data();
list($a, $b, $c) = $dataArray;
echo $a . " ". $b . " " . $c;

//if you just need 1 variable out of 3;
list(, $b, ) = $dataArray;
echo $b;

回答by Mohamed Badr

here is the best way in a similar function

这是类似功能的最佳方式

 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;
}  

and you get the array data like this

你得到这样的数组数据

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

回答by Lawson Arrington

The underlying problem revolves around accessing the data within the array, as Felix Kling points out in the first response.

正如 Felix Kling 在第一个响应中指出的那样,潜在的问题围绕着访问数组中的数据。

In the following code, I've accessed the values of the array with the print and echo constructs.

在以下代码中,我使用 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();