Connecting Tech Pros Worldwide Help | Site Map

directly using an index on returned array

  #1  
Old January 17th, 2007, 02:45 PM
Erwin Moller
Guest
 
Posts: n/a
Hi,

Consider the following code:

-------------------------
function giveArr(){
return array("one","two","three");
}

echo giveArr()[1];
// I am hoping for "two"
-------------------------

This syntax clearly doesn't work.
Does anybody know if that is possible?
If so, what is the right syntax?

(Of course I can store the returned array in some variable, and then use an
indexing on that variable.)

I am curious if the above construct is possible (in another syntax
obviously) with PHP.

TIA

Regards,
Erwin Moller

  #2  
Old January 17th, 2007, 02:55 PM
JHN
Guest
 
Posts: n/a

re: directly using an index on returned array


Just out of curiosity why would you not want to use something like


function giveArr($n){
array("one","two","three");
return array[$n];
}

echo giveArr(1);

Don't know of any other way, but I'm not a pro...

-Johan


Erwin Moller wrote:
Quote:
Hi,
>
Consider the following code:
>
-------------------------
function giveArr(){
return array("one","two","three");
}
>
echo giveArr()[1];
// I am hoping for "two"
-------------------------
>
This syntax clearly doesn't work.
Does anybody know if that is possible?
If so, what is the right syntax?
>
(Of course I can store the returned array in some variable, and then use an
indexing on that variable.)
>
I am curious if the above construct is possible (in another syntax
obviously) with PHP.
>
TIA
>
Regards,
Erwin Moller
>
  #3  
Old January 17th, 2007, 03:25 PM
Erwin Moller
Guest
 
Posts: n/a

re: directly using an index on returned array


JHN wrote:
Quote:
Just out of curiosity why would you not want to use something like
>
>
function giveArr($n){
array("one","two","three");
return array[$n];
}
>
echo giveArr(1);
No reason.
I don't actually need this right now.
And I can also store it in another var as described, and use an index on
that var.

This is just something I was trying and couldn't do this way, so I thought I
might ask in here.

Regards,
Erwin Moller
Quote:
>
Don't know of any other way, but I'm not a pro...
>
-Johan
>
>
Erwin Moller wrote:
Quote:
>Hi,
>>
>Consider the following code:
>>
>-------------------------
>function giveArr(){
> return array("one","two","three");
>}
>>
>echo giveArr()[1];
>// I am hoping for "two"
>-------------------------
>>
>This syntax clearly doesn't work.
>Does anybody know if that is possible?
>If so, what is the right syntax?
>>
>(Of course I can store the returned array in some variable, and then use
>an indexing on that variable.)
>>
>I am curious if the above construct is possible (in another syntax
>obviously) with PHP.
>>
>TIA
>>
>Regards,
>Erwin Moller
>>
  #4  
Old January 18th, 2007, 12:35 PM
Rik
Guest
 
Posts: n/a

re: directly using an index on returned array


Erwin Moller wrote:
Quote:
Hi,
>
Consider the following code:
>
-------------------------
function giveArr(){
return array("one","two","three");
}
>
echo giveArr()[1];
// I am hoping for "two"
-------------------------
>
This syntax clearly doesn't work.
Does anybody know if that is possible?
If so, what is the right syntax?
Not really. There are some options related to it, like the list()
construct.
Also, for this particular case one might use vprintf('%2$s',giveArr());
--
Rik Wasmus


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning an array inside a structure that was allocated within afunction ctj951 answers 5 November 14th, 2008 04:25 AM
removeChild skips on odd nodes, why? VK answers 6 February 8th, 2007 07:15 PM
undefined vs. undefined (was: new Array() vs []) VK answers 45 September 12th, 2006 07:15 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM