FalcorTheDog Posted August 3, 2009 Share Posted August 3, 2009 I know this is a stupid question, but let's say I have a function: getArray() that returns an array. For some reason PHP won't let you do: $myValue = getArray()[2]; if I want to get the second item in the array. Is there any way to accomplish this in one line without creating a temporary variable? Possibly something like: $myValue = get(getArray(), 2); Link to comment https://forums.phpfreaks.com/topic/168609-getting-an-array-value/ Share on other sites More sharing options...
phpSensei Posted August 3, 2009 Share Posted August 3, 2009 Not quite sure what you are trying to do. Link to comment https://forums.phpfreaks.com/topic/168609-getting-an-array-value/#findComment-889395 Share on other sites More sharing options...
FalcorTheDog Posted August 3, 2009 Author Share Posted August 3, 2009 I'm just trying to get a value from an array in one line. I already know the index, but I'm getting the array from another function. Let's say I have a function called getArray() that returns some array, and I want to access the 2nd item in that array. I realize that I can do this: $temp = getArray(); $value = $temp[2]; Basically, I just want to do this in one line of code without making a temporary variable. I would like to do this: $value = getArray()[2]; ...but php won't allow it. In Java, there is a "get" method, where you pass it an array and the index of the item you want, and it returns that item. In Java I could say: $value = getArray.get(2); ...and I just want a one-line PHP equivalent. Link to comment https://forums.phpfreaks.com/topic/168609-getting-an-array-value/#findComment-889401 Share on other sites More sharing options...
phpSensei Posted August 3, 2009 Share Posted August 3, 2009 <?php function getArray(){ $fruits = array("banana","orange","apples"); return $fruits; } $foods = getArray(); $items = $foods[1]; echo $items; ?> You mean like that? I got the 2nd argument, and it printed out oranges. Link to comment https://forums.phpfreaks.com/topic/168609-getting-an-array-value/#findComment-889405 Share on other sites More sharing options...
FalcorTheDog Posted August 3, 2009 Author Share Posted August 3, 2009 yeah, sorry for being unclear, all I want to do is this: $foods = getArray(); $items = $foods[1]; ...but in one line of code. For example, something like: $items = getArray()[1]. Is it possible? Link to comment https://forums.phpfreaks.com/topic/168609-getting-an-array-value/#findComment-889408 Share on other sites More sharing options...
Mark Baker Posted August 3, 2009 Share Posted August 3, 2009 If you want the same syntax as java, then you really should be coding in java. PHP is a different language, with its own strengths and weaknesses, and with its own syntax. If the difference between 1 line of code and 2 lines is that significant... You could modify the getArray() function to accept an index parameter function getArray($index=null){ $fruits = array("banana","orange","apples"); if (!is_null($index)) { if (isset($fruits[$index])) { return $fruits[$index]; } return false; } return $fruits; } $item = getArray(1); echo $item; Alternatively, create an array handling class Link to comment https://forums.phpfreaks.com/topic/168609-getting-an-array-value/#findComment-889414 Share on other sites More sharing options...
phpSensei Posted August 3, 2009 Share Posted August 3, 2009 yeah, sorry for being unclear, all I want to do is this: $foods = getArray(); $items = $foods[1]; ...but in one line of code. For example, something like: $items = getArray()[1]. Is it possible? I understood know, functions can take in arguments meaning you can pass down variables to it. <?php function printName($name){ print $name; } $myname = printName("Sensei"); print $myname; ?> as you can see you can give the function any data, and you can use it in the function. Hence telling the function what name to print or what index to show from an array..etc Link to comment https://forums.phpfreaks.com/topic/168609-getting-an-array-value/#findComment-889429 Share on other sites More sharing options...
Andy-H Posted August 3, 2009 Share Posted August 3, 2009 phpSensei, the $myname variable will contain a null value as the function you wrote (printName()) doesn't have a return statement, it simply prints a passed parameter when called. <?php function printName($name) { echo $name; // Faster than print; although by no significant amount... } printName('Is sufficient'."\n"); ?> Link to comment https://forums.phpfreaks.com/topic/168609-getting-an-array-value/#findComment-889466 Share on other sites More sharing options...
phpSensei Posted August 3, 2009 Share Posted August 3, 2009 phpSensei, the $myname variable will contain a null value as the function you wrote (printName()) doesn't have a return statement, it simply prints a passed parameter when called. <?php function printName($name) { echo $name; // Faster than print; although by no significant amount... } printName('Is sufficient'."\n"); ?> Oh snap typo.... lol. Havnt slept all night I showed him that way cause he wants to use the data outside of the function also. I wanted to write return name, not echo Link to comment https://forums.phpfreaks.com/topic/168609-getting-an-array-value/#findComment-889469 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.