确定可能关联数组中的第一个键的最佳方法是什么?我的第一个想法是先遍历数组,然后立即打破它,如下所示:

foreach ($an_array as $key => $val) break;

因此,$key包含第一个键,但这似乎效率很低。有人有更好的解决方案吗?


当前回答

这是我找到的最简单的方法。快速且只有两行代码:-D

$keys = array_keys($array);
echo $array[$keys[0]];

其他回答

有趣的是,foreach循环实际上是最有效的方法。

由于OP专门询问了效率,应该指出的是,目前所有的答案实际上都比foreach的效率低得多。

我用php5.4做了一个基准测试,重置/键指针方法(接受的答案)似乎比foreach慢7倍。其他处理整个数组的方法(array_keys、array_flip)显然比这还要慢,并且在处理大型数组时会变得更糟。

Foreach一点也不低效,请随意使用!

编辑2015-03-03:

基准脚本已经被要求,我没有原始脚本,而是做了一些新的测试。这次我发现foreach的速度只有reset/key的两倍。我使用了一个100键数组,并将每个方法运行了一百万次,以获得一些明显的差异,下面是简单基准测试的代码:

$array = [];
for($i=0; $i < 100; $i++)
    $array["key$i"] = $i;

for($i=0, $start = microtime(true); $i < 1000000; $i++) {
    foreach ($array as $firstKey => $firstValue) {
        break;
    }
}
echo "foreach to get first key and value: " . (microtime(true) - $start) . " seconds <br />";

for($i=0, $start = microtime(true); $i < 1000000; $i++) {
    $firstValue = reset($array);
    $firstKey = key($array);
}
echo "reset+key to get first key and value: " . (microtime(true) - $start) . " seconds <br />";

for($i=0, $start = microtime(true); $i < 1000000; $i++) {
    reset($array);
    $firstKey = key($array);
}
echo "reset+key to get first key: " . (microtime(true) - $start) . " seconds <br />";


for($i=0, $start = microtime(true); $i < 1000000; $i++) {
    $firstKey = array_keys($array)[0];
}
echo "array_keys to get first key: " . (microtime(true) - $start) . " seconds <br />";

在我的php 5.5上,此输出:

foreach to get first key and value: 0.15501809120178 seconds 
reset+key to get first key and value: 0.29375791549683 seconds 
reset+key to get first key: 0.26421809196472 seconds 
array_keys to get first key: 10.059751987457 seconds

重置+键http://3v4l.org/b4DrN/perf#tabs前肢http://3v4l.org/gRoGD/perf#tabs

为了增强Webmut的解决方案,我添加了以下解决方案:

$firstKey = array_keys(array_slice($array, 0, 1, TRUE))[0];

我在PHP 7.1上的输出是:

foreach to get first key and value: 0.048566102981567 seconds 
reset+key to get first key and value: 0.11727809906006 seconds 
reset+key to get first key: 0.11707186698914 seconds 
array_keys to get first key: 0.53917098045349 seconds 
array_slice to get first key: 0.2494580745697 seconds 

如果对大小为10000的数组执行此操作,则结果将变为

foreach to get first key and value: 0.048488140106201 seconds 
reset+key to get first key and value: 0.12659382820129 seconds 
reset+key to get first key: 0.12248802185059 seconds 
array_slice to get first key: 0.25442600250244 seconds 

array_keys方法在30秒时超时(只有1000个元素,其余元素的计时大致相同,但array_key方法大约有7.5秒)。

你可以试试

array_keys($data)[0]

因为可以使用PHP 7.3.0函数array_key_first()。

有几种方法可以为PHP7.3.0之前的版本提供此功能。可以使用array_keys(),但这可能效率很低。也可以使用reset()和key(),但这可能会更改内部数组指针。一个有效的解决方案,它不改变内部数组指针,写为polyfill:

<?php
if (!function_exists('array_key_first')) {
    function array_key_first(array $arr) {
        foreach($arr as $key => $unused) {
            return $key;
        }

        return null;
    }
}
?>

如果效率对您来说不是那么重要,您可以在PHP5.4(或更高版本)中使用array_keys($yourArray)[0]。

示例:

# 1
$arr = ["my" => "test", "is" => "best"];    
echo array_keys($arr)[0] . "\r\n"; // prints "my"


# 2
$arr = ["test", "best"];
echo array_keys($arr)[0] . "\r\n"; // prints "0"

# 3
$arr = [1 => "test", 2 => "best"];
echo array_keys($arr)[0] . "\r\n"; // prints "1"

与解决方案相比的优势:

list($firstKey) = array_keys($yourArray);

就是可以将array_keys($arr)[0]作为函数参数传递(即doSomething(array_key($arr)[0],$otherParameter))。

HTH