php:如何从数字索引中获取关联数组键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4095796/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:55:55  来源:igfitidea点击:

php: how to get associative array key from numeric index?

phpkeyassociative-array

提问by Mazatec

If I have:

如果我有:

$array = array( 'one' =>'value', 'two' => 'value2' );

how do I get the string oneback from $array[1]?

如何获取字符串one从后面$array[1]

回答by deceze

You don't. Your array doesn't have a key [1]. You could:

你没有。您的数组没有 key [1]。你可以:

  • Make a new array, which contains the keys:

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

    But the value "one" is at $newArray[0], not [1].
    A shortcut would be:

    echo current(array_keys($array));
    
  • Get the first key of the array:

     reset($array);
     echo key($array);
    
  • Get the key corresponding to the value "value":

    echo array_search('value', $array);
    
  • 创建一个包含键的新数组:

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

    但是值“一”在$newArray[0],而不是[1]
    一个快捷方式是:

    echo current(array_keys($array));
    
  • 获取数组的第一个键:

     reset($array);
     echo key($array);
    
  • 获取值“value”对应的key:

    echo array_search('value', $array);
    

This all depends on what it is exactly you want to do. The fact is, [1]doesn't correspond to "one" any which way you turn it.

这一切都取决于您究竟想要做什么。事实是,[1]无论您如何转动它,都不对应于“一个”。

回答by kennytm

$array = array( 'one' =>'value', 'two' => 'value2' );

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

Which will output:

这将输出:

one

回答by StarCrashr

If you only plan to work with one key in particular, you may accomplish this with a single line without having to store an array for all of the keys:

如果您只打算特别使用一个键,则可以用一行完成此操作,而无需为所有键存储一个数组:

echo array_keys($array)[$i];

回答by Decko

Or if you need it in a loop

或者,如果您需要循环使用它

foreach ($array as $key => $value)
{
    echo $key . ':' . $value . "\n";
}
//Result: 
//one:value
//two:value2

回答by Alex Pliutau

$array = array( 'one' =>'value', 'two' => 'value2' );
$keys  = array_keys($array);
echo $keys[0]; // one
echo $keys[1]; // two

回答by Nicolas

You might do it this way:

你可以这样做:

function asoccArrayValueWithNumKey(&$arr, $key) {
   if (!(count($arr) > $key)) return false;
   reset($array);
   $aux   = -1;
   $found = false;
   while (($auxKey = key($array)) && !$found) {
      $aux++;
      $found = ($aux == $key);
   }
   if ($found) return $array[$auxKey];
   else return false;
}

$val = asoccArrayValueWithNumKey($array, 0);
$val = asoccArrayValueWithNumKey($array, 1);
etc...

Haven't tryed the code, but i'm pretty sure it will work.

还没有尝试过代码,但我很确定它会起作用。

Good luck!

祝你好运!

回答by Ram Dane

the keyfunction helped me and is very simple

功能帮助我,很简单

回答by VivekP

If it is the first element, i.e. $array[0], you can try:

如果它是第一个元素,即$array[0],您可以尝试:

echo key($array);

If it is the second element, i.e. $array[1], you can try:

如果是第二个元素,即$array[1],您可以尝试:

next($array);
echo key($array);

I think this method is should be used when required element is the first, second or at most third element of the array. For other cases, loops should be used otherwise code readability decreases.

我认为当所需元素是数组的第一个、第二个或最多第三个元素时,应该使用这种方法。对于其他情况,应使用循环,否则代码可读性会降低。

回答by Aurovrata

Expanding on Ram Dane's answer, the keyfunction is an alternative way to get the key of the current index of the array. You can create the following function,

扩展 Ram Dane 的答案,key函数是获取数组当前索引键的另一种方法。您可以创建以下功能,

    function get_key($array, $index){
      $idx=0;
      while($idx!=$index  && next($array)) $idx++;
      if($idx==$index) return key($array);
      else return '';
    }