PHP - 获取数组值的键名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8729410/
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
PHP - Get key name of array value
提问by headacheCoder
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['firstStringName']
以便我能够循环array_keys($arr)
并'firstStringName'
通过其索引返回键字符串。我怎样才能做到这一点?
回答by zrvan
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
现在将包含 value 的键'a'
(即,'first'
)。
回答by Mark Baker
key($arr);
will return the key value for the current array element
将返回当前数组元素的键值
回答by rwb
If i understand correctly, can't you simply use:
如果我理解正确,你不能简单地使用:
foreach($arr as $key=>$value)
{
echo $key;
}
See PHP manual
参见PHP 手册
回答by Marc B
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');
回答by Rajat Singhal
Yes you can infact php is one of the few languages who provide such support..
是的,事实上 php 是少数提供这种支持的语言之一。
foreach($arr as $key=>$value)
{
}
回答by aiswarya
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));
回答by Demissew
Here is another option
这是另一种选择
$array = [1=>'one', 2=>'two', 3=>'there'];
$array = array_flip($array);
echo $array['one'];
回答by jan
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
也可以使用数字索引进行访问,例如$arr[0]
.
回答by rtroulak
you can use key function of php to get the key name:
您可以使用php的key函数来获取key名:
<?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 - 手册