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

Simply do:

array_shift(array_slice($array,0,1));
查看更多
情到深处是孤独
3楼-- · 2018-12-31 15:07

PHP 7.3 added two functions for getting the first and the last key of an array directly without modification of the original array and without creating any temporary objects:

Apart from being semantically meaningful, these functions don't even move the array pointer (as foreach would do).

Having the keys, one can get the values by the keys directly.


Examples (all of them require PHP 7.3+)

Getting the first/last key and value:

$my_array = ['IT', 'rules', 'the', 'world'];

$first_key = array_key_first($my_array);
$first_value = $my_array[$first_key];

$last_key = array_key_last($my_array);
$last_value = $my_array[$last_key];

Getting the first/last value as one-liners, assuming the array cannot be empty:

$first_value = $my_array[ array_key_first($my_array) ];

$last_value = $my_array[ array_key_last($my_array) ];

Getting the first/last value as one-liners, with defaults for empty arrays:

$first_value = empty($my_array) ? 'default' : $my_array[ array_key_first($my_array) ];

$last_value = empty($my_array) ? 'default' : $my_array[ array_key_last($my_array) ];
查看更多
余生无你
4楼-- · 2018-12-31 15:08

Use:

$first = array_slice($array, 0, 1);  
$val= $first[0];

By default, array_slice does not preserve keys, so we can safely use zero as the index.

查看更多
看风景的人
5楼-- · 2018-12-31 15:10

Suppose:

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

Just use:

$array[key($array)]

to get first element or

key($array)

to get first key.

Or you can unlink the first if you want to remove it.

查看更多
初与友歌
6楼-- · 2018-12-31 15:10
$array=array( 4 => 'apple', 7 => 'orange', 13 => 'plum' );

$firstValue = each($array)[1];

This is much more efficient than array_values() because the each() function does not copy the entire array.

For more info see http://www.php.net/manual/en/function.each.php

查看更多
谁念西风独自凉
7楼-- · 2018-12-31 15:10

Also worth bearing in mind the context in which you're doing this, as an exhaustive check can be expensive and not always necessary.

For example, this solution works fine for the situation in which I'm using it (but obviously can't be relied on in all cases...)

 /**
 * A quick and dirty way to determine whether the passed in array is associative or not, assuming that either:<br/>
 * <br/>
 * 1) All the keys are strings - i.e. associative<br/>
 * or<br/>
 * 2) All the keys are numeric - i.e. not associative<br/>
 * 
 * @param array $objects
 * @return boolean
 */
private function isAssociativeArray(array $objects)
{
    // This isn't true in the general case, but it's a close enough (and quick) approximation for the context in
    // which we're using it.

    reset($objects);
    return count($objects) > 0 && is_string(key($objects));
}
查看更多
登录 后发表回答