2

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"?

Community
  • 1
  • 1
DanSingerman
  • 36,066
  • 13
  • 81
  • 92
  • 1
    I guess you could just write a custom function for it... – deceze Oct 26 '12 at 09:31
  • I think you are out of luck with this one. It is php .. – JvdBerg Oct 26 '12 at 09:31
  • I believe that those functions in PHP are known as "constructs" which do not accept variable functions if I remember right...due to the reference usage of it – Sammaye Oct 26 '12 at 09:33
  • @Sammaye Nope, it's just that they take their arguments by reference. It has nothing to do with language constructs. – deceze Oct 26 '12 at 09:39
  • @deceze Ah OK I remember reading somewhere it was a construct but that was prolly another function. – Sammaye Oct 26 '12 at 09:40
  • IMO, there is no point in trying to come up with some fancy one-liner. Assign to temp var, like everyone else does, too. or upgrade to 5.4. – Gordon Oct 26 '12 at 09:42
  • possible duplicate of [Parentheses altering semantics of function call result](http://stackoverflow.com/q/6726589/367456) and [Strict standards error](http://stackoverflow.com/q/6726219/367456) – hakre Oct 26 '12 at 09:51

4 Answers4

5

Use this:

$var = current(return_me_an_array());

Demo: http://3v4l.org/Ruj5e

Manual: http://php.net/manual/en/function.current.php

Gordon
  • 312,688
  • 75
  • 539
  • 559
Lakatos Gyula
  • 3,949
  • 7
  • 35
  • 56
  • 2
    How does this solve the problem? EDIT: using current() won't solve it either, it requires a pass by reference – Sammaye Oct 26 '12 at 09:35
  • 1
    As far as I know current() just returns the item the array pointer is pointing at. It does not change the array so it can accept a direct array as returned by a function. – Lakatos Gyula Oct 26 '12 at 09:48
  • 1
    Confirmed it does work strangely, though the documentation is wrong: `current ( array &$array )` since it should pass as reference which means this should not function right, but it does. – Sammaye Oct 26 '12 at 10:02
  • This seems to work for me and http://3v4l.org/Ruj5e confirms that. Are there any reasons not to do this? – DanSingerman Oct 26 '12 at 10:14
1

Define your own array methods for that purpose:

function my_reset($array)
{
    return reset($array);
}

echo my_reset(return_me_an_array());

UPD:

There is even more elegant way, check Returning References manual

Vadim Ashikhman
  • 9,851
  • 1
  • 35
  • 39
0

Use this:

$var = array_shift($var = return_me_an_array());

Link to codepad

Vipin Jain
  • 1,382
  • 1
  • 10
  • 19
0

Ok can't he just supress the error? Or is that not elegant enough?

$var = @reset(return_me_an_array());

Or like shared at https://stackoverflow.com/a/6726277/881551

$var = reset( ( return_me_an_array() ) );

Both tested and work fine for the purpose of circumventing the strict notice which is what is essentially being asked.

Community
  • 1
  • 1
Anthony Hatzopoulos
  • 10,437
  • 2
  • 40
  • 57