array_key_last()
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
#1 Demo
$key = array_key_last( [ 1, 2 ] ); // 1 $key = array_key_last( [ 'one' => 1, 'two' => 2 ] ); // two
#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() array key last code WP 6.1.1
function array_key_last( array $array ) { if ( empty( $array ) ) { return null; } end( $array ); return key( $array ); }