The key() function is an inbuilt function in PHP which is used to return the index of the element of a given array to which the internal pointer is currently pointing. The current element may be starting or next element which depends on the cursor position. By default cursor position is at zero index i.e, at starting element of the given array.
Parameters: This function accepts a single parameter $array. It is the array for which we want to find the current element pointed by the internal pointer.
Return Value: It returns the index of current element of the given array. If the input array is empty then the key() function will return NULL.
Below programs illustrate the key() function in PHP:
The index of the current element of the array is: 0
The index of the current element of the array is: 1
The index of the current element of the array is: 2
- PHP | SplDoublyLinkedList key() Function
- PHP | SplObjectStorage key() Function
- PHP | SplFixedArray key() Function
- PHP | SimpleXMLIterator key() Function
- PHP | ArrayIterator key() Function
- PHP | AppendIterator key() Function
- PHP | CachingIterator key() Function
- PHP | FilesystemIterator key() Function
- PHP | DirectoryIterator key() Function
- PHP | Program to move (key,value) pair upwards or downwards
- How to delete an array element based on key in PHP?
- How to search by key=>value in a multidimensional array in PHP ?
- How to loop through an associative array and get the key in PHP?
- How to search by multiple key => value in PHP array ?
- How to remove a key and its value from an associative array in PHP ?
- How to create an array with key value pairs in PHP?
- How to check a key exists in an array in PHP ?
- D3.js nest.key() Function
- HTML | DOM KeyboardEvent key Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.