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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.