Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 17th, 2007, 02:45 PM
Erwin Moller
Guest
 
Posts: n/a
Default directly using an index on returned array

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


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles