Getting element from PHP array returned by function

后端 未结 7 1099
野趣味
野趣味 2020-12-11 01:10

I\'m not sure if this is possible, but I can\'t figure out how to do it if it is...

I want to get a specific element out of an array that is returned by a function,

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 01:51

    PHP 5.4 has added Array Dereferencing, here's the example from PHP's Array documentation:

    Example #7 Array dereferencing

    function getArray() {
        return ['a', 'b', 'c'];
    }
    
    // PHP 5.4
    $secondElement = getArray()[0]; // output = a
    
    // Previously
    $tmp = getArray();
    $secondElement = $tmp[0]; // output = a
    

提交回复
热议问题