0

Let's say that I have a library in my application that returns an array. Is it possible to access the array without, in beforehand, storing it as an variable in my scope?

The below shown code clearly doesnt work, but is something similar to this possible?

Example of what I would like to do:

if(isSet($myLibrary->create_nice_array()['element'])) {
    //...
}

Example of what I need to do right now:

$temp_array = $myLibrary->create_nice_array();

if(isSet($temp_array['element'])) {
    //...
}
hakre
  • 178,314
  • 47
  • 389
  • 754
Industrial
  • 36,181
  • 63
  • 182
  • 286
  • I know about the `is_array()`-functions though, I am looking for a general approach to saving myself from storing plently of variables in the scope that I could access in a simpler way like shown above.. – Industrial Jul 07 '11 at 11:41

4 Answers4

0

You aren't the only one who wishes they could do this in PHP!

You have to assign the output to a variable and then manipulate the variable.

Icode4food
  • 7,997
  • 13
  • 54
  • 89
0

In general, it's possible. However, you are using the isset() function:

bool isset ( mixed $var [, mixed $... ] )
Determine if a variable is set and is not NULL.

Warning: isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.

Which makes perfect sense.

Álvaro González
  • 128,942
  • 37
  • 233
  • 325
0

The isset issue:

isset can only take variables as parameters, not function returns.

The array access issue:

I think it was said pretty well here.

Because of how PHP is defined, there is no ability to have structures like: [$a]() or ()[$a]. It simply can't be done

Community
  • 1
  • 1
cwallenpoole
  • 72,280
  • 22
  • 119
  • 159
0

You could use

if(array_key_exists('element', $myLibrary->create_nice_array())) {
    //...
}
matthiasmullie
  • 2,013
  • 14
  • 17