Get the first element of an array

2018-12-31 14:30发布

I have an array:

array( 4 => 'apple', 7 => 'orange', 13 => 'plum' )

I would like to get the first element of this array. Expected result: string apple

One requirement: it cannot be done with passing by reference, so array_shift is not a good solution.

How can I do this?

标签: php arrays
30条回答
若你有天会懂
2楼-- · 2018-12-31 15:03
$first_value = reset($array); // First Element's Value
$first_key = key($array); // First Element's Key

Hope this helps. :)

查看更多
一个人的天荒地老
3楼-- · 2018-12-31 15:04

Get first element:

array_values($arr)[0]

Get last element

array_reverse($arr)[0]
查看更多
梦醉为红颜
4楼-- · 2018-12-31 15:05

I think using array_values would be your best bet here. You could return the value at index zero from the result of that function to get 'apple'.

查看更多
爱死公子算了
5楼-- · 2018-12-31 15:05

This is a little late to the game, but I was presented with a problem where my array contained array elements as children inside it, and thus I couldn't just get a string representation of the first array element. By using PHP's current() function, I managed this:

<?php
    $original = array(4 => array('one', 'two'), 7 => array('three', 'four'));
    reset($original);  // to reset the internal array pointer...
    $first_element = current($original);  // get the current element...
?>

Thanks to all the current solutions helped me get to this answer, I hope this helps someone sometime!

查看更多
初与友歌
6楼-- · 2018-12-31 15:05

This is not soo simple response in real world. Supost that we have this examples of possibles responses that you can find in some libraries.

$array1 = array();
$array2 = array(1,2,3,4);
$array3 = array('hello'=>'world', 'foo'=>'bar');
$array4 = null;

var_dump( 'reset1', reset($array1) );
var_dump( 'reset2', reset($array2) );
var_dump( 'reset3', reset($array3) );
var_dump( 'reset4', reset($array4) ); // warning

var_dump( 'array_shift1', array_shift($array1) );
var_dump( 'array_shift2', array_shift($array2) );
var_dump( 'array_shift3', array_shift($array3) );
var_dump( 'array_shift4', array_shift($array4) ); // warning

var_dump( 'each1', each($array1) );
var_dump( 'each2', each($array2) );
var_dump( 'each3', each($array3) );
var_dump( 'each4', each($array4) ); // warning

var_dump( 'array_values1', array_values($array1)[0] ); // Notice
var_dump( 'array_values2', array_values($array2)[0] );
var_dump( 'array_values3', array_values($array3)[0] );
var_dump( 'array_values4', array_values($array4)[0] ); // warning

var_dump( 'array_slice1', array_slice($array1, 0, 1) );
var_dump( 'array_slice2', array_slice($array2, 0, 1) );
var_dump( 'array_slice3', array_slice($array3, 0, 1) );
var_dump( 'array_slice4', array_slice($array4, 0, 1) );  // warning

list($elm) = $array1; //Notice
var_dump($elm);
list($elm) = $array2;
var_dump($elm);
list($elm) = $array3; // Notice
var_dump($elm);
list($elm) = $array4;
var_dump($elm);

Like you can see, we have several 'one line' solutions that work well in some cases, but not in all.

In my opinion, you have should that handler only with arrays.

Now talking about performance, assuming that we have always array, like this:

$elm = empty($array)? null : ...($array);

...you would use without errors:
$array[count($array)-1] ;
array_shift
reset
array_values
array_slice

array_shift is more fast that reset, that is more fast that [count()-1] and this three are more fast that array_values and array_slice

查看更多
宁负流年不负卿
7楼-- · 2018-12-31 15:07

Original answer, but costly (O(n)):

array_shift(array_values($array));

In O(1):

array_pop(array_reverse($array));

Edited with suggestions from comments for other use cases etc...

If modifying (in the sense of resetting array pointers) of $array is not a problem, you might use:

reset($array);

This should be theoretically more efficient, if a array "copy" is needed:

array_shift(array_slice($array, 0, 1)); 

With PHP 5.4+ (but might cause an index error if empty):

array_values($array)[0];
查看更多
登录 后发表回答