Andreas Hennings wrote on 9/7/21 19:19:
> Hello internals,
> 
> The function array_column() would be much more useful if there was an
> option to preserve the original array keys.
> I can create an RFC, but I think it is better to first discuss the options.
> 
> This is requested in different places on the web, e.g.
> https://stackoverflow.com/questions/27204590/php-array-column-how-to-keep-the-keys/39298759
> 
> A workaround is proposed here and elsewhere, using array_keys() and
> array_combine() to restore the keys.
> However, this workaround not only adds complexity, but it breaks down
> if some items don't have the value key. See https://3v4l.org/im2gZ.
> 
> A more robust workaround would be array_map(), but this is more
> complex and probably slower than array_column(), for the given
> purpose.
> 
> Some links for your convenience:
> The function was introduced in this RFC, https://wiki.php.net/rfc/array_column
> It is now documented here,
> https://www.php.net/manual/en/function.array-column.php
> 
> Some ideas how this could be fixed:
> 1. Allow a magic value (e.g. TRUE) for the $index_key parameter, that
> would cause the assoc behavior. To fully avoid BC break, this must be
> a value that previously was completely forbidden. The value TRUE is
> currently only forbidden with strict_types=1. A value of e.g. new
> \stdClass is fully forbidden, but would be weird. A constant could be
> introduced, but this would not prevent the BC concern.
> 2. Make the function preserve keys if $index_key === NULL. This would
> be a full BC break.
> 3. Add an additional parameter with a boolean option or with integer
> flags. This would be weird, because it would make the $index_key
> parameter useless.
> 4. Add a new function.
> 
> Personally I would prefer option 1, with value TRUE (I can't think of
> something better).
> 
> If I could change history, I would prefer option 2. The current
> behavior could still be achieved with array_values(array_column(..)).
> 
> Regards,
> Andreas
> 


We originally had a patch for this while PHP 5.5 was still in beta, but
we decided against merging it, and I can't remember why. :-)

https://github.com/php/php-src/pull/331

Cheers,
Ben

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to