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. Specifies an array

Return Value

This function returns FALSE on error= otherwise key.

Example

Try out following example −

<?php
   $array = array(
      'fruit1' => 'apple',
      'fruit2' => 'orange',
      'fruit3' => 'grape',
      'fruit4' => 'apple',
      'fruit5' => 'apple');
      
   while ($fruit_name = current($array)) {
      if ($fruit_name == 'apple') {
         echo key($array)."\n";
      }
      next($array);
   }
?> 

This will produce the following result −

fruit1
fruit4
fruit5

php_function_reference.htm