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

后端 未结 15 2246
栀梦
栀梦 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:35

    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;
    

提交回复
热议问题