PHP:从数组中获取密钥?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3317856/
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 from array?
提问by Industrial
I am sure that this is super easy and built-in function in PHP, but I have yet not seen it.
我确信这是 PHP 中超级简单的内置函数,但我还没有看到它。
Here's what I am doing for the moment:
这是我目前正在做的事情:
foreach($array as $key => $value) {
echo $key; // Would output "subkey" in the example array
print_r($value);
}
Could I do something like the following instead and thereby save myself from writing "$key => $value" in every foreach loop? (psuedocode)
我可以做以下类似的事情,从而避免在每个 foreach 循环中编写“$key => $value”吗?(伪代码)
foreach($array as $subarray) {
echo arrayKey($subarray); // Will output the same as "echo $key" in the former example ("subkey"
print_r($value);
}
Thanks!
谢谢!
The array:
数组:
Array
(
[subKey] => Array
(
[value] => myvalue
)
)
回答by vtorhonen
回答by Sarfraz
Use the array_searchfunction.
使用该array_search功能。
Example from php.net
来自 php.net 的示例
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
回答by Somwang Souksavatd
$foo = array('a' => 'apple', 'b' => 'ball', 'c' => 'coke');
foreach($foo as $key => $item) {
echo $item.' is begin with ('.$key.')';
}
回答by sushil bharwani
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));
回答by Dogbert
If it IS a foreachloop as you have described in the question, using $key => $valueis fast and efficient.
如果它是foreach您在问题中描述的循环,则使用$key => $value快速有效。
回答by Mike Lang
If you want to be in a foreachloop, then foreach($array as $key => $value)is definitely the recommended approach. Take advantage of simple syntax when a language offers it.
如果你想在一个foreach循环中,那么foreach($array as $key => $value)绝对是推荐的方法。当一种语言提供简单的语法时,请利用它。
回答by Chamandeep
Try this
尝试这个
foreach(array_keys($array) as $nmkey)
{
echo $nmkey;
}
回答by Anthony Rutledge
Here is a generic solution that you can add to your Array library. All you need to do is supply the associated value and the target array!
这是您可以添加到 Array 库中的通用解决方案。您需要做的就是提供关联的值和目标数组!
PHP Manual: array_search()(similiar to .indexOf() in other languages)
PHP 手册:array_search()(类似于其他语言中的 .indexOf())
public function getKey(string $value, array $target)
{
$key = array_search($value, $target);
if ($key === null) {
throw new InvalidArgumentException("Invalid arguments provided. Check inputs. Must be a (1) a string and (2) an array.");
}
if ($key === false) {
throw new DomainException("The search value does not exists in the target array.");
}
return $key;
}
回答by B Rad C
Another way to use key($array) in a foreach loop is by using next($array) at the end of the loop, just make sure each iteration calls the next() function (in case you have complex branching inside the loop)
在 foreach 循环中使用 key($array) 的另一种方法是在循环结束时使用 next($array),只需确保每次迭代都调用 next() 函数(以防循环内有复杂的分支)

