Original answer, but costly (O(n)):
(原始答案,但代价昂贵(O(n)):)
array_shift(array_values($array));
In O(1):
(在O(1)中:)
array_pop(array_reverse($array));
Other use cases, etc...
(其他用例等)
If modifying (in the sense of resetting array pointers) of $array
is not a problem, you might use:
(如果修改(在复位数组指针的意义上) $array
不是问题,则可以使用:)
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):
(使用PHP 5.4+(但如果为空,则可能导致索引错误):)
array_values($array)[0];
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…