Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grabbing a particular element from a returned array without temp var 1

Status
Not open for further replies.

miraclemaker

Programmer
Oct 16, 2002
127
GB
When I am using a function that returns an array, but I only ever want to use a particular element of that array, is there a way I can access the element of the returned array straight away, without having to use a temporary variable to store the array?

eg:
If my function getParams() returns an array, but I only ever need the third element of the array that's returned, can I do something like this:

Code:
$theparam = {getParams()}[3];

instead of 

$temp_array_var = getParams();
$theparam = $temp_array_var[3];

Thanks!
 
Yeah, the "anonymous array" feature of perl. To the best of my knowledge, this capability does not exist in PHP.

To the best of my knowledge, there is also no plan to add this to the Zend Engine 2 (with will be available with PHP 5.0).



Want the best answers? Ask the best questions: TANSTAAFL!!
 
Sounds like the place for a class or a wrapper function to me.

But to answer the question directly, I'm not familar with one either... if you find out otherwise please let us know.

-Rob
 
Sorry to double post, but I had an idea while hitting submit, this could also be a great place for an optional variable.

Code:
function getParams($index=-1) {
  ... to get params ...
  if ($index < 0 || ($index >= count($some_array))) {
    return $some_array;
  } else {
    return $some_array[$index];
  }
}

-Rob
(Sorry if you didn't care about the particulars, but figured the suggestion couldn't hurt.)

 
That would work, but I'm using a built-in function, rather than one I've written myself, so altering the function is not an option.
 
I would think
Code:
list
should work here.
Code:
list(, , $variable) = function($arguments...);
would assign
Code:
$variable
the value of the third element in the returned array.

//Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top