Logo

Programming-Idioms

History of Idiom 165 > diff from v50 to v51

Edit summary for version 51 by ancarda:
New PHP implementation by user [ancarda]

Version 50

2019-09-27, 02:54:02

Version 51

2019-09-27, 09:08:26

Idiom #165 Last element of list

Assign to variable x the last element of list items.

Idiom #165 Last element of list

Assign to variable x the last element of list items.

Code
$x = $items[array_key_last($items)];
Comments bubble
Unlike using the end function, this is functional and will not modify the internal pointer. Requires PHP 7.3+ or a polyfill
Doc URL
https://www.php.net/manual/en/function.array-key-last.php
Demo URL
https://3v4l.org/WcFVk