Re: Return an Array and immediately reference an index

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

 





Philip Thompson wrote:
Top-posting side comment: It's not nice to hijack threads.

My comments are below...

On Apr 11, 2008, at 5:33 PM, Daniel Kolbo wrote:
Hello,

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.

Thanks for your help in advance.


Perhaps these pages may assist you:

http://php.net/manual/en/function.array.php
http://php.net/functions

For more immediate help, I think you want to do something along these lines:

<?php
function returnArray ($index) {
    $arr = array('lose'=>array(5,6), 'win'=>array(9,8));
    return isset ($arr[$index]) ? $arr[$index] : 'Index not found';
}

$returnTheValueForThis = 'lose';
$result = returnArray ($returnTheValueForThis);
var_dump ($result);
?>

This var_dump will return:

array(2) { [0]=>  int(5) [1]=>  int(6) }

Hope that helps. Do some more reading in the manual to help yourself out. ;)

~Philip


Just to be sure, where you saying I hijacked a thread? If so, please educate me as to how i did this. Now to the response.

Thanks for the response.

I am familiar with the construction and returning of the arrays and referencing an index of the array. I understand your code. However, this is not what I am trying to do.

I could simply do:

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

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

To get my desired result.

I was just seeing if PHP had the capability to combine those last two lines into one. I realize the function itself is rather trivial, but just wanted some function to return an array for the sake of demonstration.

Thanks,

--
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