Get first key in a (possibly) associative array?

2019-01-01 08:05发布

What's the best way to determine the first key in a possibly associative array? My first thought it to just foreach the array and then immediately breaking it, like this:

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

Thus having $key contain the first key, but this seems inefficient. Does anyone have a better solution?

标签: php arrays
19条回答
弹指情弦暗扣
2楼-- · 2019-01-01 08:37
 $arr = array('key1'=>'value1','key2'=>'value2','key3'=>'key3');
 list($first_key) = each($arr);
 print $first_key;
 // key1
查看更多
登录 后发表回答