Re: Array map with reset function

From: Date: Thu, 27 Aug 2020 10:14:29 +0000
Subject: Re: Array map with reset function
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-111700@lists.php.net to get a copy of this message
On Wed, Aug 26, 2020 at 11:16 AM Rowan Tommins <rowan.collins@gmail.com> wrote: > > On Tue, 25 Aug 2020 at 20:08, Nikita Popov <nikita.ppv@gmail.com> wrote: > > > On Tue, Aug 25, 2020 at 7:47 PM Michael Voříšek - ČVUT FEL < > > vorismi3@fel.cvut.cz> wrote: > > > > > The following code stopped working in PHP 8: > > > > > > https://3v4l.org/UlIE3 > > > > To be pedantic: it didn't stop working, it just started raising Warnings. > The return value of array_map remains the same. > > > > > > > > > > > is it a bug or a feature? > > > > > > > This is an intentional change, if that's what you're asking. array_map() is > > not spec'd to pass by reference, so it doesn't. > > > > > As with many uses of reset(), this one doesn't really need to be by > reference, it's being used for its output. Unfortunately, array_value_first > failed at RFC [https://wiki.php.net/rfc/array_key_first_last#vote], so I > presume the recommended replacement code in the general case is this: > > array_map(fn($x)=>$x[array_key_first($x)], $rows) > > Of course for a list-style array like the example shown, it can be a lot > simpler: > > array_map(fn($x)=>$x[0], $rows) > > Regards, > -- > Rowan Tommins > [IMSoP] array_map(fn($x)=>reset($x), $rows) would work as well Alex

« previous php.internals (#111700) next »