PHP中的key()函数
该key()函数从数组中获取键。它返回内部指针指向的数组元素的键。
语法
key(arr)
参数
arr-要使用的数组。
返回
该key()函数返回内部指针指向的数组元素的键。
示例
以下是一个例子-
<?php
$arr = array("Electronics","Footwear");
echo "Key from the current position = " . key($arr);
?>输出结果
Key from the current position = 0
示例
让我们看另一个例子-
<?php
$arr = array(
'one' => 'tim',
'two' => 'peter',
'three' => 'david');
while ($val = current($arr)) {
if ($val == 'peter') {
echo key($arr);
}
next($arr);
}
?>输出结果
two