Re: [PHP] Return an Array and immediately reference an index

2008-04-12 Thread Jim Lucas

Bojan Tesanovic wrote:


On Apr 12, 2008, at 12:33 AM, 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.

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





This is not possible in PHP, though you can have a Array wrapper class

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

}
var_dump (returnarray()-offsetGet('lose'));


or even better make you own wrapper class with __set() and __get()  
methods so you can have


var_dump (returnarray()-lose);

of course only in PHP5


Well, not quite so fast saying this is only possible in PHP5

You could do something like this.

?php

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

print_r(returnHash()-lose);

?

Basically, this converts your newly built array into an object, using 
the stdClass object.


Then reference the index via an object variable name instead of an array 
style access method.







Bojan Tesanovic
http://www.carster.us/









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



Re: [PHP] Return an Array and immediately reference an index

2008-04-12 Thread Stut

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



Re: [PHP] Return an Array and immediately reference an index

2008-04-12 Thread Daniel Kolbo



Jim Lucas wrote:

Bojan Tesanovic wrote:


On Apr 12, 2008, at 12:33 AM, 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.

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





This is not possible in PHP, though you can have a Array wrapper class

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

}
var_dump (returnarray()-offsetGet('lose'));


or even better make you own wrapper class with __set() and __get()  
methods so you can have


var_dump (returnarray()-lose);

of course only in PHP5


Well, not quite so fast saying this is only possible in PHP5

You could do something like this.

?php

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

print_r(returnHash()-lose);

?

Basically, this converts your newly built array into an object, using 
the stdClass object.


Then reference the index via an object variable name instead of an array 
style access method.







Bojan Tesanovic
http://www.carster.us/











Thanks, I appreciate your comment Bojan
DanK

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



Re: [PHP] Return an Array and immediately reference an index

2008-04-12 Thread Daniel Kolbo



Stut wrote:

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



Thanks Stut.  By chance do you know of any proposed syntax for this 
feature?  Or, what syntax would seem logical to you?


DanK

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



Re: [PHP] Return an Array and immediately reference an index

2008-04-12 Thread Stut

On 12 Apr 2008, at 15:18, Daniel Kolbo wrote:

Stut wrote:

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


Thanks Stut.  By chance do you know of any proposed syntax for this  
feature?  Or, what syntax would seem logical to you?


I'm sure I've seen it discussed on the internals list but I don't know  
if anything has been agreed. Your best bet is to search the archives  
for the internals list.


-Stut

--
http://stut.net/

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



Re: [PHP] Return an Array and immediately reference an index

2008-04-12 Thread Nathan Nobbe
On Fri, Apr 11, 2008 at 6:33 PM, Daniel Kolbo [EMAIL PROTECTED] wrote:

search the archives ;)

http://www.mail-archive.com/php-general@lists.php.net/msg224626.html

-nathan


Re: [PHP] Return an Array and immediately reference an index

2008-04-12 Thread Casey
On Sat, Apr 12, 2008 at 9:12 AM, Nathan Nobbe [EMAIL PROTECTED] wrote:
 On Fri, Apr 11, 2008 at 6:33 PM, Daniel Kolbo [EMAIL PROTECTED] wrote:

  search the archives ;)

  http://www.mail-archive.com/php-general@lists.php.net/msg224626.html

  -nathan
?php
function ReturnArray() {
return array('a' = 'f', 'b' = 'g', 'c' = 'h', 'd' = 'i', 'e' = 'j');
}

echo ${!${!1}=ReturnArray()}['a']; // 'f'
?

:)
-- 
-Casey

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



Re: [PHP] Return an Array and immediately reference an index

2008-04-12 Thread Nathan Nobbe
On Sat, Apr 12, 2008 at 12:18 PM, Casey [EMAIL PROTECTED] wrote:

 On Sat, Apr 12, 2008 at 9:12 AM, Nathan Nobbe [EMAIL PROTECTED]
 wrote:
  On Fri, Apr 11, 2008 at 6:33 PM, Daniel Kolbo [EMAIL PROTECTED] wrote:
 
   search the archives ;)
 
   http://www.mail-archive.com/php-general@lists.php.net/msg224626.html
 
   -nathan
 ?php
 function ReturnArray() {
return array('a' = 'f', 'b' = 'g', 'c' = 'h', 'd' = 'i', 'e' =
 'j');
 }

 echo ${!${!1}=ReturnArray()}['a']; // 'f'
 ?


ya; i never did sit down and try to figure out how that works; care to
explain ?

-nathan


Re: [PHP] Return an Array and immediately reference an index

2008-04-12 Thread Casey
On Sat, Apr 12, 2008 at 9:35 AM, Nathan Nobbe [EMAIL PROTECTED] wrote:

 On Sat, Apr 12, 2008 at 12:18 PM, Casey [EMAIL PROTECTED] wrote:


 
 
 
  On Sat, Apr 12, 2008 at 9:12 AM, Nathan Nobbe [EMAIL PROTECTED]
 wrote:
   On Fri, Apr 11, 2008 at 6:33 PM, Daniel Kolbo [EMAIL PROTECTED] wrote:
  
search the archives ;)
  
http://www.mail-archive.com/php-general@lists.php.net/msg224626.html
  
-nathan
  ?php
  function ReturnArray() {
 return array('a' = 'f', 'b' = 'g', 'c' = 'h', 'd' = 'i', 'e' =
 'j');
  }
 
  echo ${!${!1}=ReturnArray()}['a']; // 'f'
  ?

 ya; i never did sit down and try to figure out how that works; care to
 explain ?

 -nathan


?php
echo ${!${!1}=ReturnArray()}['a'];

${!${!1}=ReturnArray()}['a']
 !1 resolves to false.
${!${false}=ReturnArray()}['a']
 false resolves to... I don't know. Let's just say false resolves to a.
${!$a=ReturnArray()}['a']
 $a is now the array. The ! changes the returned array into the
boolean false (like: if (!$handle = fopen('x', 'r')) { echo
'connection failed' }.
${false}['a']
 I don't know what false resolves to, but we're using a.
$a['a']
?
-- 
-Casey

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



Re: [PHP] Return an Array and immediately reference an index

2008-04-12 Thread Daniel Kolbo



Casey wrote:

On Sat, Apr 12, 2008 at 9:35 AM, Nathan Nobbe [EMAIL PROTECTED] wrote:

On Sat, Apr 12, 2008 at 12:18 PM, Casey [EMAIL PROTECTED] wrote:





On Sat, Apr 12, 2008 at 9:12 AM, Nathan Nobbe [EMAIL PROTECTED]

wrote:

On Fri, Apr 11, 2008 at 6:33 PM, Daniel Kolbo [EMAIL PROTECTED] wrote:

 search the archives ;)

 http://www.mail-archive.com/php-general@lists.php.net/msg224626.html

 -nathan

?php
function ReturnArray() {
   return array('a' = 'f', 'b' = 'g', 'c' = 'h', 'd' = 'i', 'e' =

'j');

}

echo ${!${!1}=ReturnArray()}['a']; // 'f'
?

ya; i never did sit down and try to figure out how that works; care to
explain ?

-nathan



?php
echo ${!${!1}=ReturnArray()}['a'];

${!${!1}=ReturnArray()}['a']
 !1 resolves to false.
${!${false}=ReturnArray()}['a']
 false resolves to... I don't know. Let's just say false resolves to a.
${!$a=ReturnArray()}['a']
 $a is now the array. The ! changes the returned array into the
boolean false (like: if (!$handle = fopen('x', 'r')) { echo
'connection failed' }.
${false}['a']
 I don't know what false resolves to, but we're using a.
$a['a']
?


Just awesome!  Thanks for the explanation Casey, and thanks for the 
archived link Nathan.  I knew I'd learn something by asking.


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



Re: [PHP] Return an Array and immediately reference an index

2008-04-12 Thread Bojan Tesanovic


On Apr 12, 2008, at 6:18 PM, Casey wrote:

On Sat, Apr 12, 2008 at 9:12 AM, Nathan Nobbe  
[EMAIL PROTECTED] wrote:
On Fri, Apr 11, 2008 at 6:33 PM, Daniel Kolbo [EMAIL PROTECTED]  
wrote:


 search the archives ;)

 http://www.mail-archive.com/php-general@lists.php.net/msg224626.html

 -nathan

?php
function ReturnArray() {
return array('a' = 'f', 'b' = 'g', 'c' = 'h', 'd' = 'i',  
'e' = 'j');

}

echo ${!${!1}=ReturnArray()}['a']; // 'f'
?

:)
--
-Casey

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





WOW!! PHP always surprises  me, this is the pros of PHP not being  
strict type language.


Igor Jocic
http://www.carster.us/






[PHP] Return an Array and immediately reference an index

2008-04-11 Thread Daniel Kolbo

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.

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



Re: [PHP] Return an Array and immediately reference an index

2008-04-11 Thread Philip Thompson

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

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



Re: [PHP] Return an Array and immediately reference an index

2008-04-11 Thread Daniel Kolbo



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



Re: [PHP] Return an Array and immediately reference an index

2008-04-11 Thread Philip Thompson

On Apr 11, 2008, at 6:31 PM, Daniel Kolbo wrote:


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.


If you are viewing a message (in this case, the thread entitled  
Quarters -- ERRORS --) and you hit Reply and change the message to  
whatever (in this case, Return an Array and immediately reference an  
index), it still shows up in the same thread as the Quarters one.  
This implies that your email has to do with the Quarters one... but it  
really doesn't. =D


So, in order to fix this, just hit New instead of Reply. =D No harm  
done, just good listserv netiquette for people reading their emails  
that shove the *same content* emails together. We're all here to  
learn, right.




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,


Oooh. I see what you're saying now. Sorry for the confusion. To my  
knowledge (which is limited ;), I don't think you can do that. This is  
somewhat similar to some desired functionality in PHP - I don't  
remember the name of it. For example,


?php
class f1() {
  function f2() {
echo Hello;
  }

  function f3() {
echo World!;
  }
}

new f1()-f2() . new f1()-f3();
?

Something like that. Basically, call a class/function and get the  
contents directly without instantiating it first. Maybe others have an  
opinion on this


~Philip

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



Re: [PHP] Return an Array and immediately reference an index

2008-04-11 Thread Bojan Tesanovic


On Apr 12, 2008, at 12:33 AM, 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.

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





This is not possible in PHP, though you can have a Array wrapper class

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

}
var_dump (returnarray()-offsetGet('lose'));


or even better make you own wrapper class with __set() and __get()   
methods so you can have


var_dump (returnarray()-lose);

of course only in PHP5


Bojan Tesanovic
http://www.carster.us/