如何遍历关联数组并获取密钥? [英] How to loop through an associative array and get the key?
本文介绍了如何遍历关联数组并获取密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的关联数组:
$arr = array(
1 => "Value1",
2 => "Value2",
10 => "Value10"
);
使用以下代码,$v
填充$arr
的值
Using the following code, $v
is filled with $arr
's values
foreach($arr as $v){
echo($v); // Value1, Value2, Value10
}
我如何获得 $arr
的密钥?
How do I get $arr
's keys instead?
foreach(.....){
echo($k); // 1, 2, 10
}
推荐答案
您可以:
foreach ($arr as $key => $value) {
echo $key;
}
如PHP 文档中所述.
这篇关于如何遍历关联数组并获取密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文