Possible Duplicate:
php explode and array index
In PHP 5.3…
I have a dynamic static method that always returns an array. In some cases it will return an array containing only one element. I want to assign, in one statement, the first element of the array to a variable. At the moment I have to do this:
$user = User::findByEmail($_SESSION['email']);
$user = $user[0];
echo $user;
I want to avoid the part…
$user = $user[0];
I expect something like this to work:
$user = User::findByEmail($_SESSION['email'])[0]; // not working
or
$user = User::findByEmail($_SESSION['email'])->get(0); // not working
or
$user = User::findByEmail($_SESSION['email']).get(0); // not working
EDIT
The solution you want is this:
Note that using
current()with functions always returns the first element, because the array was never assigned to a variable, and does not have a point. This is NOT the case if the function returns an array by reference.