PHP - Function key()


Advertisements


Syntax

key ( $array );

Definition and Usage

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

Parameters

Sr.No Parameter & Description
1

array(Required)

It specifies an array

Return Value

This function returns FALSE on error= otherwise key.

Example

Try out following example −

Live Demo
<?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);
   }
?> 

This will produce the following result −

f1
f4
f5

php_function_reference.htm

Advertisements