Jump to content

"Getting" an array value


FalcorTheDog

Recommended Posts

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

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.

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

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

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");

?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.