Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

key

Description

The key of Array for PHP fetch a key from an array.

Syntax

key(
    array|object $array
): int|string|null

Parameters

array

The array.

Return

Returns the key of the array element that's currently being pointed to by the internal pointer.

It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, it returns null.

Examples

1 · first

<?

$array = array("a" => 0, "b" => 1);

$return = key($array);

echo $return;

?>
a

2 · next

<?

$array = array("a" => 0, "b" => 1);

next($array);

$return = key($array);

echo $return;

?>
b

3 · for

<?

$array = array("a" => 0, "b" => 1, "c" => 2, "d" => 3, "e" => 4);

for($i = 0; $i < count($array); ++$i)
{
    $return = key($array);

    echo $return;

    next($array);
}

?>
abcde

4 · foreach · value

<?

$array = array("a" => 0, "b" => 1, "c" => 2, "d" => 3, "e" => 4);

foreach($array as $value)
{
    $return = key($array);

    echo $return;

    next($array);
}

?>
abcde

5 · foreach · key value

<?

$array = array("a" => 0, "b" => 1, "c" => 2, "d" => 3, "e" => 4);

foreach($array as $key => $value)
{
    echo $key;
}

?>
abcde

6 · while

<?

$array = array("a" => 0, "b" => 1, "c" => 2, "d" => 3, "e" => 4);

$i = 0;

while($i < count($array))
{
    $return = key($array);

    echo $return;

    next($array);
    
    ++$i;
}

?>
abcde
HomeMenu