The array_key_first helps you to get the first key of an array directly in PHP. It saves time and removes extra steps in your code.
Table of Content
What is the PHP array_key_first Function
PHP array_key_first gives the first key of an array. It works on indexed arrays and associative arrays. It returns null when the array is empty.
array_key_first(array $array)This syntax shows that it takes one array and gives the first key or null.
The function looks at the array and reads the first key. It does not change the array and does not loop through every value.
Here is a quick example:
$items = ['apple', 'banana', 'cherry'];
$firstKey = array_key_first($items);
echo $firstKey; // 0This code sets an array of items and then takes its first key. It prints the key of the first element.
PHP array_key_first with Indexed Arrays
This function reads the first index of a normal array and returns it. It works even when the array starts with a custom index number.
Here is an example:
$data = [5 => 'cat', 6 => 'dog', 7 => 'bird'];
echo array_key_first($data); // 5The code sets an indexed array with custom keys and prints the first key.
PHP array_key_first with Associative Arrays
This function also works on associative arrays. It gives the first string key instead of a number key.
Here is an example:
$user = ['name' => 'Ali', 'age' => 30, 'city' => 'Jeddah'];
echo array_key_first($user); // nameThe code sets an associative array with three keys and prints the first key.
PHP array_key_first vs array_keys Comparison
array_key_first gives the first key of an array directly. array_keys gives all keys of an array as a list.
Use array_key_first when you only need one key and the array_keys when you need every key.
| Function | Output Type | Purpose |
|---|---|---|
| array_key_first | Single key | Get first key only |
| array_keys | Array | Get every key in one array |
Examples
Get First Key from Indexed Array:
$numbers = [10, 20, 30];
echo array_key_first($numbers); // 0This example shows how to get the first key from a simple numeric array. It returns the key of the first value.
Get First Key from Associative Array:
$settings = ['theme' => 'dark', 'layout' => 'wide'];
echo array_key_first($settings); // themeThis example shows how to get the first key from an associative array. It returns the key of the first pair in that array.
Check Empty Array before Using array_key_first:
$data = [];
$key = array_key_first($data);
var_dump($key); // NULLThis example shows how the function returns null when the array has no values. You can check it before use.
Work with Mixed Keys:
$mix = [2 => 'x', 'b' => 'y', 3 => 'z'];
echo array_key_first($mix); // 2This example shows how it handles mixed key types. It still gives the first key even if the array has numbers and strings.
Wrapping Up
You learned what PHP array_key_first does.
Here is a quick recap:
- It gives the first key of an array, and works on indexed or associative arrays.
- It returns
nullfor empty arrays, and it differs fromarray_keysby returning only one key.
FAQs
What does PHP array_key_first Function do?
$array = ["red" => 1, "blue" => 2, "green" => 3];
echo array_key_first($array);
Output:
redHow to use PHP array_key_first with indexed arrays?
$array = [10, 20, 30, 40];
echo array_key_first($array);
Output:
0
- The array starts with index 0.
array_key_firstreturns 0 as the first key.
What is the difference between array_key_first and array_keys?
array_key_firstreturns only the first key.array_keysreturns all keys as an array.
$array = ["x" => 5, "y" => 6, "z" => 7];
print_r(array_key_first($array));
// Output: x
print_r(array_keys($array));
// Output: Array ( [0] => x [1] => y [2] => z )Can array_key_first return null in PHP?
array_key_first returns null when the array is empty.
$array = [];
var_dump(array_key_first($array));
Output:
NULLSimilar Reads
Type casting in PHP refers to the process of converting a variable from one data type to another. This is…
The callback in PHP lets you pass functions as arguments and reuse code. It helps you to control how tasks…
PHP array_intersect_assoc compares values and keys in arrays. It returns matches with the same key and value from all arrays…
The array_pop function in PHP gives you a direct way to take the last element from an array. Understand the…
PHP array_push lets you add one or more values to the end of an array. It appeared to make adding…
actions, store settings, or even create log files for debugging. That’s where PHP fwrite comes in. It writes data to…
Classes and objects are important parts of OOP in PHP. They help you organize your code so you can update…
It’s very important to remember user data for each session when building web applications. This enables a high level of…
When you first step into web development, one name seems to pop up everywhere: PHP. The PHP programming language has…
PHP has great tools to handle external files such as opening, writing, deleting and more else, in this tutorial we…