如何在PHP中找到关联数组中第一个元素的值?

如何在PHP中找到关联数组中第一个元素的值?

问题描述:

I have an array like this:

$array = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => 'grape',
);

Is there a function that'll grab 'apple' (the first key) from that array? Or do I have no choice but to do this?

function firstkey($array)
{
    for($array as $first)
    {
        return $first;
    }
}

我有一个这样的数组: p>

  $ array = 数组(
'fruit1'=>'apple',
'fruit2'=>'orange',
'fruit3'=>'grape',
); 
  code>   pre> 
 
 

是否有一个函数可以从该数组中获取'apple' code>(第一个键)? 或者我别无选择只能这样做? p>

  function firstkey($ array)
 {
 for($ array as $ first)
 {
 return  $ first; 
} 
} 
  code>  pre> 
  div>