# Array map with reset function

- Thread: https://externals.io/message/111684
- Original archive: https://news-web.php.net/php.internals/111684
- Started by vorismi3@fel.cvut.cz on 2020-08-25T17:47:08+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #111684 — vorismi3@fel.cvut.cz — 2020-08-25T17:47:08+00:00

URL: https://externals.io/message/111684#111684

The following code stopped working in PHP 8: 

https://3v4l.org/UlIE3 

is it a bug or a feature? 

is posting issues like this to internals@lists.php.net email prefered
over opening bug directly? or is there any special email for it? 

With kind regards / Mit freundlichen Grüßen / S přátelským pozdravem,

Michael Voříšek

---

## #111685 — Levi Morrison via internals — 2020-08-25T18:52:33+00:00

URL: https://externals.io/message/111684#111685 · In reply to: #111684

On Tue, Aug 25, 2020 at 11:47 AM Michael Voříšek - ČVUT FEL
<vorismi3@fel.cvut.cz> wrote:
>
> The following code stopped working in PHP 8:
>
> https://3v4l.org/UlIE3
>
> is it a bug or a feature?
>
> is posting issues like this to internals@lists.php.net email prefered
> over opening bug directly? or is there any special email for it?
>
> With kind regards / Mit freundlichen Grüßen / S přátelským pozdravem,
>
> Michael Voříšek

The documentation for array_map doesn't mention references at all. I'm
inclined to believe it to be an intentional warning, but it may be a
regression.

I recommend using array_walk, as it _does_ mention reference support.

---

## #111686 — Nikita Popov — 2020-08-25T19:08:32+00:00

URL: https://externals.io/message/111684#111686 · In reply to: #111684

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
>
> 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.

is posting issues like this to internals@lists.php.net email prefered
> over opening bug directly? or is there any special email for it?
>

Please prefer reporting issues at bugs.php.net over this mailing list. (At
least for bugs -- for non-trivial feature requests, the mailing list may be
better.)

Regards,
Nikita

---

## #111692 — Rowan Tommins — 2020-08-26T08:16:12+00:00

URL: https://externals.io/message/111684#111692 · In reply to: #111686

> On Tue, Aug 25, 2020 at 7:47 PM Michael Voříšek - ČVUT FEL <
> vorismi3@fel.cvut.cz> wrote:
>
> > […]



To be pedantic: it didn't stop working, it just started raising Warnings.
The return value of array_map remains the same.



>
> > […]
>
> 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]

---

## #111700 — drealecs@gmail.com — 2020-08-27T10:14:29+00:00

URL: https://externals.io/message/111684#111700 · In reply to: #111692

>
> 
>
> > […]
>
>
>
> To be pedantic: it didn't stop working, it just started raising Warnings.
> The return value of array_map remains the same.
>
>
>
> > […]
>
>
> 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
