English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The key() function retrieves the key name from the associated array
key ($array);
The key() function returns the element key from the current internal pointer position.
Serial Number | Parameters and Description |
---|---|
1 | array (required) It specifies an array |
The key() function returns the key name of the current element pointed to by the internal pointer in the array. However, it does not move the pointer. If the internal pointer exceeds the end of the element list or the array is empty, key() will return NULL.
<?php $fruit = array( 'f1' => 'apple', 'f2' => 'orange', 'f3' => 'grape', 'f4' => 'apple', 'f5' => 'apple'); while ($f_name = current($fruit)) { if ($f_name == 'apple') { echo key($fruit)."\n"; } next($fruit); } ?>Test and See‹/›
Output Result:
f1 f4 f5