在 PHP 中返回关联数组的第一个键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6698428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return first key of associative array in PHP
提问by Dan Lugg
I'm trying to obtain the first key of an associative array, without creating a temporary variable via array_keys()
or the like, to pass by reference. Unfortunately both reset()
and array_shift()
take the array argument by reference, so neither seem to be viable results.
我正在尝试获取关联数组的第一个键,而不是通过创建临时变量array_keys()
等来通过引用传递。不幸的是两者reset()
并array_shift()
采取通过引用数组参数,所以既不似乎是可行的结果。
With PHP 5.4 I'll be in heaven; array_keys($array)[0];
, but unfortunately this of course is not an option either.
使用 PHP 5.4 我会在天堂;array_keys($array)[0];
,但不幸的是,这当然也不是一种选择。
I could create a function to serve the purpose, but I can only imagine there is some concoction of PHP's array_*
functions that will produce the desired result in a single statement, that I cannot think of or come up with.
我可以创建一个函数来达到目的,但我只能想象有一些 PHParray_*
函数的组合可以在单个语句中产生所需的结果,这是我无法想到或想出的。
So:
所以:
$array = array('foo' => 'bar', 'hello' => 'world');
$firstKey = assorted_functions($array); // $firstKey = 'foo'
The reason for the "no reference" clause in my question is only for the fact that I assume array_keys()
will be required (if there is a way passing by reference, please fire away)
我的问题中“无参考”条款的原因仅是因为我认为array_keys()
需要这样做(如果有通过参考传递的方式,请开火)
I'd use key()
, but that requires a reset()
as I'm not sure where the pointer will be at the time of this operation.
我会使用key()
,但这需要一个,reset()
因为我不确定执行此操作时指针的位置。
Addendum
附录
I'm following up on a realization I had recently: as I mentioned in the comments, it'll use the memory all the same, so if that's a concern, this question hath no solution.
我正在跟进我最近的一个认识:正如我在评论中提到的,它会使用相同的内存,所以如果这是一个问题,这个问题没有解决方案。
$a = range(0,99999);
var_dump(memory_get_peak_usage()); // int(8644416)
$k = array_keys($a)[0];
var_dump(memory_get_peak_usage()); // int(17168824)
I knewthis, as PHP doesn't have such optimization capabilities, but figured it warranted explicit mention.
我知道这一点,因为 PHP 没有这样的优化功能,但认为它值得明确提及。
The brevity of the accepted answer is nice though, and'll work if you're working with reasonablysized arrays.
不过,接受的答案的简洁性很好,如果您正在使用合理大小的数组,它将起作用。
回答by adlawson
Although array_shift(array_keys($array));
will work, current(array_keys($array));
is faster as it doesn't advance the internal pointer.
虽然array_shift(array_keys($array));
可以工作,但current(array_keys($array));
速度更快,因为它不会推进内部指针。
Either one will work though.
任一个都行。
Update
更新
As @TomcatExodus noted, array_shift();
expects an array passed by reference, so the first example will issue an error. Best to stick with current();
正如@TomcatExodus 所指出的,array_shift();
期望通过引用传递数组,因此第一个示例将发出错误。最好坚持current();
回答by Brian Graham
回答by RiaD
array_shift(array_keys($array))
回答by Orbling
回答by PrinsEdje80
What about using array_slice (in combination with array_keys for associative arrays)?
如何使用 array_slice(结合 array_keys 用于关联数组)?
$a = range(0,999999);
var_dump(memory_get_peak_usage());
$k = array_keys(array_slice($a, 0, 1, TRUE))[0];
var_dump(memory_get_peak_usage());
var_dump($k);
$k = array_keys($a)[0];
var_dump(memory_get_peak_usage());
Gives as output (at least with me):
作为输出给出(至少对我而言):
int(36354360)
int(36355112)
int(0)
int(72006024)
int(0)