PHP array_key_first() Function
The PHP array_key_first() function returns the first key of the given array without affecting the internal array pointer.
Syntax
array_key_first(array)
Parameters
array |
Required. Specify an array. |
Return Value
Returns the first key of array if the array is not empty; null otherwise.
Exceptions
NA.
Example:
The example below shows the usage of array_key_first() function.
<?php $Arr = array("Red" => 1, "Green" => 2, "Blue" => 3); //printing all keys of the array using //array_key_first and array_shift functions $n = sizeof($Arr); for($i = 0; $i < $n; $i++) { //displaying first element echo array_key_first($Arr)."\n"; //popping first element array_shift($Arr); } ?>
The output of the above code will be:
Red Green Blue
Example:
Consider one more example where the array_key_first() function is used with an index array.
<?php $Arr = array_fill(-2, 5, 10); print_r($Arr); echo "\n"; //displaying the first key (first index) echo "First index: ".array_key_first($Arr); ?>
The output of the above code will be:
Array ( [-2] => 10 [-1] => 10 [0] => 10 [1] => 10 [2] => 10 ) First index: -2
❮ PHP Array Reference