Tutorials Point


  Learning PHP
  Advanced PHP
  PHP Function Reference
  PHP Useful Resources
  Selected Reading

© 2011 TutorialsPoint.COM


  Home     References     Discussion Forums     About TP  

PHP Function key()


previous

Syntax

key ( $array );

Definition and Usage

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

Paramters

ParameterDescription
arrayRequired. 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).'<br />';
    }
    next($array);
}
?> 

This will produce following result:

fruit1
fruit4
fruit5

previous Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names