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

Two solutions for you.

Solution 1 - Just use the key. You have not said, that you can not use it. :)

<?php
// get first element of this array. 
$array = array( 4 => 'apple', 7 => 'orange', 13 => 'plum' );

// gets the first element by key
$result = $array[4];

//Expected result: string apple
assert('$result === "apple" /* Expected result: string apple. */');
?>

Solution 2 - array_flip() + key()

<?php
// get first element of this array. Expected result: string apple
$array = array( 4 => 'apple', 7 => 'orange', 13 => 'plum' );

// turn values to keys
$array = array_flip($array);

// you might, thrown an reset in
// just to make sure that the array pointer is at first element
// also reset return the first element
// reset($myArray);

// return first key 
$firstKey = key($array); 

assert('$firstKey === "apple" /* Expected result: string apple. */');
?>

Solution 3 - array_keys()

echo $array[array_keys($array)[0]];
查看更多
回忆,回不去的记忆
3楼-- · 2018-12-31 15:16

As Mike pointed out (the easiest possible way):

$arr = array( 4 => 'apple', 7 => 'orange', 13 => 'plum' )
echo reset($arr); //echoes "apple"

If you want to get the key: (execute it after reset)

echo key($arr); //echoes "4"

From PHP's documentation:

mixed reset ( array &$array );

Description:

reset() rewinds array's internal pointer to the first element and returns the value of the first array element, or FALSE if the array is empty.

查看更多
千与千寻千般痛.
4楼-- · 2018-12-31 15:16

PHP 5.4+:

array_values($array)[0];
查看更多
有味是清欢
5楼-- · 2018-12-31 15:19

You can get Nth element with a language construct "list":

// 1st item
list($firstItem) = $yourArray;

// 1st item from an array that is returned from function
list($firstItem) = functionThatReturnsArray();

// 2nd item
list( , $secondItem) = $yourArray;

with array_keys function you can do the same for keys:

list($firstKey) = array_keys($yourArray);
list(, $secondKey) = array_keys($yourArray);
查看更多
梦该遗忘
6楼-- · 2018-12-31 15:20

From Laravel's helpers:

function head($array)
{
    return reset($array);
}

The array being passed by value to the function, the reset() affects the internal pointer of a copy of the array, it doesn't touch the original array. (note it returns false if the array is empty)

Usage example:

$data = ['foo', 'bar', 'baz'];

current($data); // foo
next($data); // bar
head($data); // foo
next($data); // baz


Also, here is an alternative. It's very marginally faster, but more interesting, it lets easily change the default value if the array is empty:

function head($array, $default = null)
{
    foreach ($array as $item) {
        return $item;
    }

    return $default;
}
查看更多
人气声优
7楼-- · 2018-12-31 15:20

Keep this simple! Lots of correct answers here, but to minimize all the confusion. These two work and reduce a lot of overhead.

key($array) = gets the first key of an array
current($array) = gets the first value of an array

查看更多
登录 后发表回答