Senči
Senči

Reputation: 941

How is the beautiful way to reference something in an array returned by a function?

I am trying to reference something inside an array i get from a function.

Lets say I have a collection named $myArrays and when i call $myArrays->first(); I will get an array.

Now when I try to get the first element in my array with $myArrays->first()[0] this doesn't work. Why is that so and is there a way to use it in a similar way?

Regards, Senad

Upvotes: 1

Views: 68

Answers (1)

phant0m
phant0m

Reputation: 16905

Because you are not using PHP 5.4:

Function array dereferencing has been added, e.g. foo()[0]

You need a temporary variable:

$first = $myArrays->first();
$first[0]

And no, you cannot "trick" PHP this way either:

($myArray->first())[0]

Upvotes: 4

Related Questions