views:

712

answers:

18

Hi - any idea how if the following is possible in PHP as a single line ?:

<?php
$firstElement = functionThatReturnsAnArray()[0];

... It doesn't seem to 'take'. I need to do this as a 2-stepper:

<?php
$allElements = functionThatReturnsAnArray();
$firstElement = $allElements[0];

... just curious - other languages I play with allow things like this, and I'm lazy enoug to miss this in PHP ... any insight appreciated ...

+6  A: 

Unfortunately, that is not possible with PHP. You have to use two lines to do it.

owenmarshall
Yeah, this is one of the flaws with PHP.
grom
well, it's a pain, but it's not a show-stopper really is it? I mean, given the reputation of PHP for poorly-formed and badly managed code, maybe it's a good thing! :p
nickf
A: 

As far as I know this is not possible, I have wanted to do this myself several times.

Unkwntech
+5  A: 

Try:

<?php
$firstElement = reset(functionThatReturnsAnArray());

If you're just looking for the first element of the array.

calebbrown
That's really cool. Thanks!
Mark Biek
agreed - and in my case this actually works pretty well. Thx so much for this!
awesome, never read the manual for reset()
Darryl Hein
Lovely. Huge refactoring tonight then!
Ken
It works but now you have to spend extra brain cycles to translate from reset(func()) to func()[0] when you look at the code.
Pavel Chuchuva
A: 

http://us3.php.net/reset

Only available in php version 5.

Jack B Nimble
definitely works in PHP4
nickf
+2  A: 

list() is useful here. With any but the first array element, you'll need to pad it with useless variables. For example:

list( $firstElement ) = functionThatReturnsAnArray();
list( $firstElement , $secondElement ) = functionThatReturnsAnArray();

And so on.

Scott Reynen
+8  A: 

@Scott Reynen

that's not true. This will work:

list(,,$thirdElement) = $myArray;
nickf
A: 

nickf, good to know, thanks. Unfortunately that has readability problems beyond a few commas.

Scott Reynen
yeah that's very true - i avoid it whenever I can, but it's still possible! :)
nickf
A: 

If it's always the first element, you should probably think about having the function return just the first item in the array. If that is the most common case, you could use a little bit of coolness:

function func($first = false) {
    ...
    if $first return $array[0];
    else return $array;
}

$array = func();
$item = func(true);

My php is slightly rusty, but i'm pretty sure that works.

You can also look at array_shift() and array_pop().

This is probably also possible:

array(func())[0][i];

The 0 is for the function.

neouser99
+4  A: 

You can do this in one line! Use array_shift().

<?php

echo array_shift(i_return_an_array());

function i_return_an_array() {
    return array('foo', 'bar', 'baz');
}

When this is executed, it will echo "foo".

Garrett Albright
+1  A: 

Either current($array) or array_shift($array) will work, the former will leave the array intact.

Greg
A: 

Well, I have found a couple of ways to get what you want without calling another function.

$firstElement = ($t = functionThatReturnsAnArray()) ? $t[0] : false;

and for strings you could use

$string = (($t = functionThatReturnsAnArray())==0) . $t[0];

.. Interesting problem

Draco

Draco777
A: 

You can use array_slice(), like so:

$elementX = array_slice(functionThatReturnsAnArray(), $x, 1);

Also noticed that end() is not mentioned. It returns the last element of an array.

A: 

someone said something about using reset()... why not use the reset function?

Quamis
A: 

I think any of the above would require a comment to explain what you're doing, thus becoming two lines. I find it simpler to do:

$element = functionThatReturnsArray();
$element = $element[0];

This way, you're not using an extra variable and it's obvious what you're doing.

adam
A: 
$firstItem = current(returnsArray());
deceze
A: 

Sometimes I'll change the function, so it can optionally return an element instead of the entire array:

<?php
function functionThatReturnsAnArray($n = NULL) {
  return ($n === NULL ? $myArray : $myArray[$n]);
}
$firstElement = functionThatReturnsAnArray(0);
JW
A: 

I actually use a convenience function i wrote for such purposes:

/**
 * Grabs an element from an array using a key much like array_pop
 */
function array_key_value($array, $key) {
    if(!empty($array) && array_key_exists($key, $array)) {
  return $array[$key];
 }
 else {
  return FALSE;
 }
}

then you just call it like so:

$result = array_key_value(getMeAnArray(), 'arrayKey');
SeanDowney
A: 

I am guessing that this is a built-in or library function, since it sounds like you cannot edit it directly. I recommend creating a wrapper function to give you the output you need:

function functionThatReturnsOneElement( $arg ) { $result = functionThatReturnsAnArray( $arg ); return $result[0]; }

$firstElement = functionThatReturnsOneElement();