PHP: Can I reference a single member of an array t

2020-02-12 03:07发布

问题:

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:

Try:

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

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



回答2:

@Scott Reynen

that's not true. This will work:

list(,,$thirdElement) = $myArray;


回答3:

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



回答4:

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



回答5:

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.



回答6:

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


回答7:

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.



回答8:

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



回答9:

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



回答10:

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.



回答11:

$firstItem = current(returnsArray());


回答12:

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



回答13:

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


回答14:

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



回答15:

http://us3.php.net/reset

Only available in php version 5.



回答16:

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.



回答17:

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