获取数组值的键名

时间:2023-02-12 22:07:23

I have an array as the following:

我有一个数组如下所示:

function example() {
    /* some stuff here that pushes items with
        dynamically created key strings into an array */

    return array( // now lets pretend it returns the created array
        'firstStringName' => $whatEver,
        'secondStringName' => $somethingElse
    );
}

$arr = example();

// now I know that $arr contains $arr['firstStringName'];

I need to find out the index of $arr['firstStringName'] so that I am able to loop through array_keys($arr) and return the key string 'firstStringName' by its index. How can I do that?

我需要找到$arr的索引[' firstname '],以便能够循环遍历array_keys($arr)并根据索引返回键字符串'firstStringName'。我怎么做呢?

10 个解决方案

#1


185  

If you have a value and want to find the key, use array_search() like this:

如果您有一个值并希望找到该键,那么使用array_search()如下:

$arr = array ('first' => 'a', 'second' => 'b', );
$key = array_search ('a', $arr);

$key will now contain the key for value 'a' (that is, 'first').

$key现在将包含值“a”(即“first”)的键。

#2


39  

key($arr);

will return the key value for the current array element

将返回当前数组元素的键值吗

http://uk.php.net/manual/en/function.key.php

http://uk.php.net/manual/en/function.key.php

#3


27  

If i understand correctly, can't you simply use:

如果我理解正确,你就不能简单地使用:

foreach($arr as $key=>$value)
{
  echo $key;
}

See PHP manual

看到PHP手册

#4


13  

If the name's dynamic, then you must have something like

如果名称是动态的,那么必须有类似的东西

$arr[$key]

which'd mean that $key contains the value of the key.

这意味着$key包含键的值。

You can use array_keys() to get ALL the keys of an array, e.g.

可以使用array_keys()获取数组的所有键,例如。

$arr = array('a' => 'b', 'c' => 'd')
$x = array_keys($arr);

would give you

会给你

$x = array(0 => 'a', 1 => 'c');

#5


11  

Yes you can infact php is one of the few languages who provide such support..

的确,php是为数不多的提供此类支持的语言之一。

foreach($arr as $key=>$value)
{

}

#6


6  

use array_keys() to get an array of all the unique keys.

使用array_keys()获取所有唯一键的数组。

Note that an array with named keys like your $arr can also be accessed with numeric indexes, like $arr[0].

注意,可以使用数字索引(如$arr[0])访问具有命名键(如$arr)的数组。

http://php.net/manual/en/function.array-keys.php

http://php.net/manual/en/function.array-keys.php

#7


2  

if you need to return an array elements with same value, use array_keys() function

如果需要返回具有相同值的数组元素,请使用array_keys()函数

$array = array('red' => 1, 'blue' => 1, 'green' => 2);
print_r(array_keys($array, 1));

#8


2  

Here is another option

这是另一个选择

$array = [1=>'one', 2=>'two', 3=>'there'];
$array = array_flip($array);
echo $array['one']; 

#9


0  

Check the documentation for array_keys() function

检查array_keys()函数的文档

array_keys Function

中的函数

#10


0  

you can use key function of php to get the key name:

您可以使用php的key function获取key name:

<?php
    $array = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => 'grape',
    'fruit4' => 'apple',
    'fruit5' => 'apple');

    // this cycle echoes all associative array
    // key where value equals "apple"
    while ($fruit_name = current($array)) {
      if ($fruit_name == 'apple') {
        echo key($array).'<br />';
      }
    next($array);
     }
?>

like here : PHP:key - Manual

比如这里:PHP:key - Manual

#1


185  

If you have a value and want to find the key, use array_search() like this:

如果您有一个值并希望找到该键,那么使用array_search()如下:

$arr = array ('first' => 'a', 'second' => 'b', );
$key = array_search ('a', $arr);

$key will now contain the key for value 'a' (that is, 'first').

$key现在将包含值“a”(即“first”)的键。

#2


39  

key($arr);

will return the key value for the current array element

将返回当前数组元素的键值吗

http://uk.php.net/manual/en/function.key.php

http://uk.php.net/manual/en/function.key.php

#3


27  

If i understand correctly, can't you simply use:

如果我理解正确,你就不能简单地使用:

foreach($arr as $key=>$value)
{
  echo $key;
}

See PHP manual

看到PHP手册

#4


13  

If the name's dynamic, then you must have something like

如果名称是动态的,那么必须有类似的东西

$arr[$key]

which'd mean that $key contains the value of the key.

这意味着$key包含键的值。

You can use array_keys() to get ALL the keys of an array, e.g.

可以使用array_keys()获取数组的所有键,例如。

$arr = array('a' => 'b', 'c' => 'd')
$x = array_keys($arr);

would give you

会给你

$x = array(0 => 'a', 1 => 'c');

#5


11  

Yes you can infact php is one of the few languages who provide such support..

的确,php是为数不多的提供此类支持的语言之一。

foreach($arr as $key=>$value)
{

}

#6


6  

use array_keys() to get an array of all the unique keys.

使用array_keys()获取所有唯一键的数组。

Note that an array with named keys like your $arr can also be accessed with numeric indexes, like $arr[0].

注意,可以使用数字索引(如$arr[0])访问具有命名键(如$arr)的数组。

http://php.net/manual/en/function.array-keys.php

http://php.net/manual/en/function.array-keys.php

#7


2  

if you need to return an array elements with same value, use array_keys() function

如果需要返回具有相同值的数组元素,请使用array_keys()函数

$array = array('red' => 1, 'blue' => 1, 'green' => 2);
print_r(array_keys($array, 1));

#8


2  

Here is another option

这是另一个选择

$array = [1=>'one', 2=>'two', 3=>'there'];
$array = array_flip($array);
echo $array['one']; 

#9


0  

Check the documentation for array_keys() function

检查array_keys()函数的文档

array_keys Function

中的函数

#10


0  

you can use key function of php to get the key name:

您可以使用php的key function获取key name:

<?php
    $array = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => 'grape',
    'fruit4' => 'apple',
    'fruit5' => 'apple');

    // this cycle echoes all associative array
    // key where value equals "apple"
    while ($fruit_name = current($array)) {
      if ($fruit_name == 'apple') {
        echo key($array).'<br />';
      }
    next($array);
     }
?>

like here : PHP:key - Manual

比如这里:PHP:key - Manual