PHP函数返回数组 [英] PHP function return array

查看:162
本文介绍了PHP函数返回数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

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

函数data(){

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

返回数组($ a,$ b,$ c);
}


?>

如何获得 $ a , $ b $ c 通过调用上述函数?



=h2_lin>解决方案

 函数data(){
$ out ['a'] =abc;
$ out ['b'] =def;
$ out ['c'] =ghi;
返回$ out;
}

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


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, $c by calling the above function?

解决方案

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'];

这篇关于PHP函数返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆