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

key

Description

The key of Array for PHP fetches 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 · current

<?

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

$return = key($array);

echo $return;
a

2 · next

<?

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

next($array);

$return = key($array);

echo $return;
b

3 · for

<?

$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 =
[
    "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 =
[
    "a" => 0,
    "b" => 1,
    "c" => 2,
    "d" => 3,
    "e" => 4
];

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

6 · while

<?

$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