473,809 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

directly using an index on returned array

Hi,

Consider the following code:

-------------------------
function giveArr(){
return array("one","tw o","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
3 1235
JHN
Just out of curiosity why would you not want to use something like
function giveArr($n){
array("one","tw o","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","tw o","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
JHN wrote:
Just out of curiosity why would you not want to use something like
function giveArr($n){
array("one","tw o","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","tw o","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
Rik
Erwin Moller wrote:
Hi,

Consider the following code:

-------------------------
function giveArr(){
return array("one","tw o","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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
12094
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound to know this number after the first FETCH, isn't it? On a side note, why queries using LIMIT are SO terribly slow, compared to cursors and sometimes even ones without LIMIT? Shouldn't LIMIT be internally implemented using cursor mechanism then?...
2
6564
by: Paul Reddin | last post by:
Hi, (V8.1 Fp2) Our application uses JDBC batch to execute mutiple insert statements and we saw a strange thing this morning. There were 4 SQL Insert statements in the batch, and we know the 2nd would have violated a Unique Constraint and failed, but the SQL error returned was related to the 3rd statement (which had a FK dependent on the 2nd
2
24713
by: David Billson | last post by:
Hi all, Fairly straight forward problem. I have a Post using HttpWebRequest that posts data to a server. That server then posts data back to me. Posting the data's not a problem, however I'm wondering if there is an easy way to read back the variables that are returned. If not, I'm left creating a parse routine which is not the end of the world but I just want to be as programatically correct as I can be. Code:
1
2493
by: Pradeep Kumar | last post by:
I have a scenario where I am writing a property(using get and set) have to return a member of an object stored in an array of objects! The class user has to pass an index based on which a object i's member is returned from the array. How will the property be declared in C#?
2
2069
by: sri2097 | last post by:
Hi all, I have to select a particular file (using the 'Browse') button in Windows. After this I need to populate the 'Open Dialogue Box' with the path of the file I need (I have the entier path of the file I need). Then I need to select the 'Open' Button. Only after this the file I want is attached. Any idea as to how this can be done using 'Win32 API's.
6
2242
by: routeslip | last post by:
I'm refering to an entry in an array by it's string key, as in foo. Is there a way to get the numeric index of that array without iterating through the entire array? What I need to do is find the "next" or "previous" entry in the array, but the keys are strings, instead of numerical indeces. thanks for your help.
14
3052
by: oLgAa25 | last post by:
Hello all, I am back I have this question everything compiles fine, but I just need to check for an index that is more than the array size. Although it says out of bound, but it is still deleting the last element. Can someone help please Thank you all Olga I just wanted to test my function,so don't bother with main, and print
7
9840
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
11
6410
by: =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post by:
The following code is in a custom deserializer: object value = (int) 1; string nameToParse = Enum.GetName(field.FieldType, value); value = Enum.Parse(field.FieldType, nameToParse); Currently we follow the path below: intValue --enum name --enum value
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.