Tác dụng của hàm array_key_first()
The array_key_first() function gets the first key of an array.
The following table summarizes the technical details of this function.
| Return Value: | Returns the first key of array if the array is not empty; NULL otherwise. |
|---|---|
| Version: | PHP 7.3.0+ |
Syntax
The basic syntax of the array_key_first() function is given with:
The following example shows the array_key_first() function in action.
Example
Run this code »<?php
// Sample array
$persons = array("Harry"=>18, "Clark"=>32, "John"=>24);
// Getting the first key from the persons array
echo array_key_first($persons); // Prints: Harry
?>
Parameters
The array_key_first() function accepts the following parameters.
| Parameter | Description |
|---|---|
| array | Required. Specifies the array to be used. |
More Examples
Here're some more examples showing how array_key_first() function basically works:
This function can also be used with the indexed array, as shown here:
Example
Run this code »<?php
// Creating an indexed array
$colors[1] = "red";
$colors[2] = "green";
$colors[3] = "blue";
// Getting the first index from the colors array
echo array_key_first($colors); // Prints: 1
?>