Question

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 ...

Was it helpful?

Solution

Try:

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

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

OTHER TIPS

@Scott Reynen

that's not true. This will work:

list(,,$thirdElement) = $myArray;

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

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

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.

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');

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.

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

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

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.

$firstItem = current(returnsArray());

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

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();

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

http://us3.php.net/reset

Only available in php version 5.

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.

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top