By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,729 Members | 1,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,729 IT Pros & Developers. It's quick & easy.

directly using an index on returned array

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

Jan 17 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
JHN
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:
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
Jan 17 '07 #2

P: n/a
JHN wrote:
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
>
Don't know of any other way, but I'm not a pro...

-Johan
Erwin Moller wrote:
>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
Jan 17 '07 #3

P: n/a
Rik
Erwin Moller wrote:
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
Jan 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.