English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP key() Function Usage and Example

PHP Array Function Manual

The key() function retrieves the key name from the associated array

Syntax

key ($array);

Definition and Usage

The key() function returns the element key from the current internal pointer position.

Parameter

Serial NumberParameters and Description
1

array (required)

It specifies an array

Return Value

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.

Online Example

<?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

  PHP Array Function Manual