array_key_last()WP 5.9.0

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

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

1 time — 0.000003 sec (speed of light) | 50000 times — 0.0001 sec (speed of light)

No Hooks.

Return

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

Usage

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

Examples

0

#1 Demo

$key = array_key_last( [ 1, 2 ] ); // 1

$key = array_key_last( [ 'one' => 1, 'two' => 2 ] ); // two
0

#2 Not affect the internal array pointer

An example of how the internal array pointer is stored.

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

echo key( $arr ); // one

echo array_key_last( $arr ); // two

echo key( $arr ); // one

Changelog

Since 5.9.0 Introduced.

array_key_last() code WP 6.4.3

function array_key_last( array $array ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
	if ( empty( $array ) ) {
		return null;
	}

	end( $array );

	return key( $array );
}