Access an Array Returned by a Function [duplicate]

PHP 5.4 added array Dereferencing, here’s the example from PHP’s Array documentation:

Example #7 Array dereferencing

function getArray() {
    return array(1, 2, 3);
}

// on PHP 5.4
$secondElement = getArray()[1];

// previously
$tmp = getArray();
$secondElement = $tmp[1];

Leave a Comment