Monday, 26 August 2013

PHP Function key()

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

No comments:

Post a Comment