11

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

1

17 Answers 17

9

@Scott Reynen

that's not true. This will work:

list(,,$thirdElement) = $myArray;
Sign up to request clarification or add additional context in comments.

Comments

8

Try:

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

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

3 Comments

agreed - and in my case this actually works pretty well. Thx so much for this!
Lovely. Huge refactoring tonight then!
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.
6

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

3 Comments

Yeah, this is one of the flaws with PHP.
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
actually, the other answers pointing me to list & reset work well - not pretty, but they do what i want them to :-) I love this web site !
5

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

Comments

3

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.

Comments

1

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

Comments

1

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.

Comments

1

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

Comments

0

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

1 Comment

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

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.

Comments

0
$firstItem = current(returnsArray());

Comments

0

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

Comments

0

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

Comments

-1

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

Comments

-1

https://www.php.net/reset

Only available in php version 5.

Comments

-1

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.

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.