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

时间:2021-11-23 21:34:21

If I have:

如果我有:

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

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

如何从$array[1]取回字符串1 ?

8 个解决方案

#1


93  

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

你不。你的数组没有键[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:

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

    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":

    得到值“value”对应的键:

    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]并不对应于“1”无论你怎么改变它。

#2


48  

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

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

Which will output:

将输出:

one

#3


12  

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];

#4


3  

Or if you need it in a loop

或者如果你需要循环

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

#5


2  

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

#6


0  

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!

好运!

#7


0  

the key function helped me and is very simple

键函数对我有帮助,而且非常简单

#8


-1  

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

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

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

#1


93  

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

你不。你的数组没有键[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:

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

    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":

    得到值“value”对应的键:

    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]并不对应于“1”无论你怎么改变它。

#2


48  

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

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

Which will output:

将输出:

one

#3


12  

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];

#4


3  

Or if you need it in a loop

或者如果你需要循环

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

#5


2  

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

#6


0  

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!

好运!

#7


0  

the key function helped me and is very simple

键函数对我有帮助,而且非常简单

#8


-1  

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

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

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