PHP array_key_first Function: How it Works with Examples

php array_key_first

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.

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; // 0

This 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); // 5

The 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); // name

The 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.

FunctionOutput TypePurpose
array_key_firstSingle keyGet first key only
array_keysArrayGet every key in one array

Examples

Get First Key from Indexed Array:

$numbers = [10, 20, 30];
echo array_key_first($numbers); // 0

This 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); // theme

This 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); // NULL

This 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); // 2

This 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 null for empty arrays, and it differs from array_keys by returning only one key.

FAQs

What does PHP array_key_first Function do?

PHP array_key_first Function returns the first key of an array. It helps when you need the first element position. Example:
$array = ["red" => 1, "blue" => 2, "green" => 3];
echo array_key_first($array); 
Output: red

How 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_first returns 0 as the first key.

What is the difference between array_key_first and array_keys?

  1. array_key_first returns only the first key.
  2. array_keys returns all keys as an array.
Example:
$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?

Yes, array_key_first returns null when the array is empty.
$array = [];
var_dump(array_key_first($array));
Output: NULL

Similar Reads

PHP Array: Accessing and Managing Elements

PHP arrays are lists or maps that contain multiple values grouped by array keys. They may include integers, strings, booleans,…

PHP array_change_key_case: Change Array Key Cases

The array_change_key_case function in PHP changes all array keys to lowercase or uppercase for consistent data use. Understand the array_change_key_case…

PHP Type Hints: Ensuring Data Integrity

PHP-type hinting is something that keeps your code in check and helps your functions receive just the right types of…

Parameters and Arguments in PHP: What’s the Difference?

If you're coding in PHP you've most probably come across the terms 'parameters' and 'arguments' in functions. Well, they are…

PHP array_intersect_ukey Function: How it Works with Examples

The array_intersect_ukey is used to compare arrays by keys with a custom function in PHP. It returns matches by keys…

PHP Predefined Constants Explained Simply

PHP has a number of predefined constants that give you some information about the language and its environment. You can…

PHP array_shift: Remove the First Array Element with Examples

The array_shift function in PHP takes the first element from an array and returns it. It also moves all other…

Learn How to Insert Documents in MongoDB Using PHP

Inserting documents into your MongoDB collections is one of the most basic but important tasks when working with PHP. In…

PHP break Statement: How to Exit a Loop

The PHP break statement helps in controlling the flow of loop work. It gives you a way to stop a…

PHP Hello World: Write Your First Program with Code Example

You always start with “Hello World” when you learn a new language. It keeps things simple. You see right away…

Previous Article

Node.js File System with Examples

Next Article

HTML meta http-equiv Attribute for Web SEO with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.