Home >>PHP Array Functions >PHP key() Function

PHP key() Function

PHP key() Function

PHP key() function is used to get the index of the element of a given input array to which the internal pointer is currently pointing. It accepts only a single parameter $array that is the given input array. It returns the index of current element of the given array as output and NULL if the input array is empty

Syntax:
key($array);

Parameter Values

Parameter Description
array array This is a required parameter. This parameter defines the array to use.
Here is an example of key() function in PHP:
<html>
<body>
<?php
$x=array("A","B","C","D");
echo "The key from the current position is: ".key($x);
?>
</body>
</html>
Output:
The key from the current position is: 0
Example 2:
<html>
<body>
<?php
$x=array("A","B","C","D");
next($x);
next($x);
echo "The key from the current position is: ".key($x);
?>
</body>
</html>
Output:
The key from the current position is: 2

No Sidebar ads