Re: [RFC][Under Discussion] Add functions array_key_first()and array_key_last()
| From: | Rowan Collins | Date: | Wed, 20 Jun 2018 14:09:21 +0000 |
| Subject: | Re: [RFC][Under Discussion] Add functions array_key_first()and array_key_last() | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-102323@lists.php.net to get a copy of this message | ||
On 20 June 2018 at 14:55, Levi Morrison <levim@php.net> wrote:
>
> if ([$key, $value] = array_last($input) {
> // do something
> }
>
>
Unfortunately, this won't work. I didn't know list() could even be used as
an expression, but apparently if it is, it evaluates to the whole array on
its right-hand side, which makes sense.
So, if array_last([]) returns [null, null], then [$key,
$value]=array_last([]) will also evaluate to [null, null]. Since that's not
an empty array (it has two elements in it), it will evaluate to true.
https://3v4l.org/lIEa1
If, on the other hand, array_last([]) returned something empty, like [],
the list() assignment would throw Notices for the implicit access to
elements [0] and [1] of the empty array. https://3v4l.org/qhq5R
Regards,
--
Rowan Collins
[IMSoP]