Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

PHP array_key_first function

This function retrieves the first key of the provided array. It does not affect the pointer’s position within the array.

Description / Descripción
array_key_first(array $array): int | string | null

Parameter

$array – the array provided

Return

Returns the first key of the array if it is not empty; otherwise, it will return null.

Example

The following example is a simple one with car brands:

$marcasDeAutos = array(
    "Toyota",
    "Ford",
    "Honda",
    "Chevrolet",
    "Volkswagen",
    "BMW",
    "Mercedes-Benz",
    "Audi",
    "Nissan",
    "Hyundai",
    "Kia",
    "Subaru",
    "Mazda",
    "Lexus",
    "Jeep",
);

echo(array_key_first($marcasDeAutos));
0
When could you use array_key_first? Well, it depends on whether you want to treat the first element differently or not. Not always in an array are the elements of equal importance. For example, the first element could be the title, or maybe you want to do something visual, like the header in a blog post that displays whether it’s internally checked, the author, whether it’s completed, and if there’s a peer review.
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category