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:22

Some arrays don't work with functions like list, reset or current. Maybe they're "faux" arrays - partially implementing ArrayIterator, for example.

If you want to pull the first value regardless of the array, you can short-circuit an iterator:

foreach($array_with_unknown_keys as $value) break;

Your value will then be available in $value and the loop will break after the first iteration. This is more efficient than copying a potentially large array to a function like array_unshift(array_values($arr)).

You can grab the key this way too:

foreach($array_with_unknown_keys as $key=>$value) break;

If you're calling this from a function, simply return early:

function grab_first($arr) {
    foreach($arr as $value) return $value;
}
查看更多
流年柔荑漫光年
3楼-- · 2018-12-31 15:23
$myArray = array (4 => 'apple', 7 => 'orange', 13 => 'plum');
$arrayKeys = array_keys($myArray);

// the first element of your array is:
echo $myArray[$arrayKeys[0]];  
查看更多
与风俱净
4楼-- · 2018-12-31 15:23

Most of these work! BUT for a quick single line (low resource) call:

$array = array( 4 => 'apple', 7 => 'orange', 13 => 'plum' );
echo $array[key($array)];

// key($array) -> will return the first key (which is 4 in this example)

Although this works, and decently well, please also see my additional answer: https://stackoverflow.com/a/48410351/1804013

查看更多
查无此人
5楼-- · 2018-12-31 15:26

Old post but anyway... I imagine the author just was looking for a way to get the first element of array after getting it from some function (mysql_fetch_row for example) without generating a STRICT "Only variables should be passed by reference". If it so, almos all ways described here will get this message... and some of them uses a lot of additional memory duplicating an array (or some part of it). An easy way to avoid it is just assigning the value inline before calling any of those functions:

$first_item_of_array = current($tmp_arr = mysql_fetch_row(...));
// or
$first_item_of_array = reset($tmp_arr = func_get_my_huge_array());

This way you don't get the STRICT message on screen neither in logs and you don't create any additional arrays. It works with both indexed AND associative arrays

查看更多
唯独是你
6楼-- · 2018-12-31 15:28
$arr = array( 9 => 'apple', 7 => 'orange', 13 => 'plum' );
echo reset($arr); // echoes 'apple'

If you don't want to lose the current pointer position, just create an alias for the array.

查看更多
路过你的时光
7楼-- · 2018-12-31 15:29

current($array)

can get you first element of an array, according to PHP Manual

Every array has an internal pointer to its "current" element, which is initialized to the first element inserted into the array.

So it works until you have re-positioned array pointer, otherwise you ll have to reset the array.

查看更多
登录 后发表回答