Re: Return an Array and immediately reference an index

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 12 Apr 2008, at 00:31, Daniel Kolbo wrote:
Philip Thompson wrote:
On Apr 11, 2008, at 5:33 PM, Daniel Kolbo wrote:
I want to return an array from function and reference an index all in one line. Is this possible?

In the code below I want I want $yo to be the array(5,6).

Here is what I've tried,

function returnarray() {
   return array('lose' => array(5,6), 'win' => array(9,8));
}

$yo = returnarray()['lose'];
var_dump($yo);

This yields a parse error.

function returnarray() {
   return array('lose' => array(5,6), 'win' => array(9,8));
}

$yo = {returnarray()}['lose'];
var_dump($yo);

This yields a parse error.

function returnarray() {
   return array('lose' => array(5,6), 'win' => array(9,8));
}

$yo = ${returnarray()}['lose'];
var_dump($yo);

This gives notices as the result of returnarray() is being converted to a string. $yo === NULL...not what i want.

function returnarray() {
   return array('lose' => array(5,6), 'win' => array(9,8));
}

$yo = returnarray()->['lose'];
var_dump($yo);

This yields a parse error.

function returnarray() {
   return array('lose' => array(5,6), 'win' => array(9,8));
}

$yo = ${returnarray()}->['lose'];
var_dump($yo);

This yields a parse error.

The PHP parser does not support this, but you may see it in a future version - it's a commonly requested feature.

There are various ways to code around this limitation as other posters have stated but to me they all add far too much processing to make it worth saving a line of code and a temporary variable.

-Stut

--
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux