Given a function which returns an array;
function return_me_an_array() {
return array('my_value');
}
How can I call it and get a single value (whether, first, last or only) from the array in an elegant fashion (yes I know this is PHP!)
If I do this
$var = reset(return_me_an_array());
I get the following PHP strict error:
Strict warning: Only variables should be passed by reference
Likewise each of these give the same warning.
$var = array_shift(return_me_an_array());
$var = array_pop(return_me_an_array());
I know I can do this:
$temp = return_me_an_array();
$var = $reset($temp);
But having to do it over two statements is pretty horrible.
Is there a good way to do this?
On the face of it this is similar to How to return an array and get the first element of it in one line in PHP? but the accepted answer uses reset
- so I think this is different; I'm either looking for a solution that doesn't give strict warnings, or to be told what I want is impossible.
I'm also aware the PHP 5.4 has introduced array dereferencing (e.g. return_me_an_array()[0]
) but alas I am currently using 5.3.
Edit after being closed: How is this too localized? This is a general question about how to write elegant PHP. Or is it the case that so few people care about elegant PHP it is "an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet"?