How to access the elements of a function's return array?

后端 未结 15 2222
栀梦
栀梦 2020-12-05 03:46

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



        
15条回答
  •  死守一世寂寞
    2020-12-05 04:08

    Your function is:

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

    It returns an array where position 0 is $a, position 1 is $b and position 2 is $c. You can therefore access $a by doing just this:

    data()[0]

    If you do $myvar = data()[0] and print $myvar, you will get "abc", which was the value assigned to $a inside the function.

提交回复
热议问题