array_key_first()WP 5.9.0

Get the first key of the given array without affecting the internal array pointer.

Get the first key of the given array without affecting the internal array pointer.

1 time — -0.00003 sec (speed of light) | 50000 times — 0.00 sec (speed of light)

No Hooks.

Return

String|Int|null. The first key of array if the array is not empty; null otherwise.

Usage

array_key_first( $array );
$array(array) (required)
An array.

Examples

1

#1 Demo [auto-translate]

$first_key = array_key_first( [ 1, 2 ] ); // 0

$first_key = array_key_first( [ 'one' => 1, 'two' => 2 ] ); // one
1

#2 An example of how the internal array pointer is stored. [auto-translate]

$arr = [
	'one' => 1,
	'two' => 2
];
next( $arr ); // switch the internal array pointer

echo key( $arr ); // two

echo array_key_first( $arr ); // one

echo key( $arr ); // two

Changelog

Since 5.9.0 Introduced.

array_key_first() code WP 6.5.2

function array_key_first( array $array ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
	foreach ( $array as $key => $value ) {
		return $key;
	}
}