Bug #74077 [Com]: Array_map with null argument inconsistency between one array and multiple arrays
Edit report at https://bugs.php.net/bug.php?id=74077&edit=1
ID: 74077
Comment by: vivekkumarshiv7 at gmail dot com
Reported by: remyfox at hotmail dot com
Summary: Array_map with null argument inconsistency between
one array and multiple arrays
Status: Open
Type: Bug
Package: Arrays related
Operating System: Windows 10
PHP Version: 7.1.1
Block user comment: N
Private report: N
New Comment:
Health Care Magzine are sharing latest news about treatment etc. More info to
visit:(https://healthcaremagzine.com)github.com
Previous Comments:
------------------------------------------------------------------------
[2021-06-06 23:17:36] a at b dot c dot de
I just ran into this myself on 8.0.7. The thing was I was receiving an list (as an array) of an
uncertain number of tuples and needed to transpose them (to get a tuple of arrays).
All was fine until I received only one tuple. For my purposes the consistent result of transposing
[[a,b]] would have been [[a],[b]] but as described the actual behaviour meant I got [a,b] and things
went haywire from there.
Admittedly I was already handling the special case of zero, so my fix has been to just add another
special case, but the change in behaviour of array_map(null, ...$array) when the length of $array
just happens to be 1 -- i.e., that array_map(null, $array) is a no-op -- did come as a surprise.
------------------------------------------------------------------------
[2017-02-11 21:22:26] requinix@php.net
(Optional explanation: https://git.io/vDwnB)
The behavior has been present since PHP 4, so simply changing it is probably not a good idea and
fixing the documentation would be easier.
But array_map was created in a time when it wasn't easy to make and use a one-off helper
function like it is today. Deprecating using a null callback is also a possibility here as the
replacement is just a one-line anonymous function. I don't think it's too late to add this
to 7.2 along with the few others happening at the same time...
------------------------------------------------------------------------
[2017-02-11 18:38:01] cmb@php.net
Alternative reproducer: <https://3v4l.org/DauMC>.
Not sure if that qualifies as bug (at least changing the behavior
would cause a considerable BC break), because the behavior is not
well documented at all.
------------------------------------------------------------------------
[2017-02-10 13:36:12] remyfox at hotmail dot com
Description:
------------
In the array map documentation (http://php.net/manual/en/function.array-map.php) under #4 there is
an example of using null as argument so as to make an array of arrays. This works fine for as long
one item is provided to the array_map function. However, if there are is just one item provided,
then the function will fail to be consistent.
Test script:
---------------
$two_item_array = [["abc", "def"], ["ghi", "jkl"]];
$one_item_array = [["abc", "def"]];
print_r(array_map(null, ...$two_item_array));
print_r(array_map(null, ...$one_item_array));
Expected result:
----------------
Array
(
[0] => Array
(
[0] => abc
[1] => ghi
)
[1] => Array
(
[0] => def
[1] => jkl
)
)
Array
(
[0] => Array
(
[0] => abc
)
[1] => Array
(
[0] => def
)
)
Actual result:
--------------
Array
(
[0] => Array
(
[0] => abc
[1] => ghi
)
[1] => Array
(
[0] => def
[1] => jkl
)
)
Array
(
[0] => abc
[1] => def
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74077&edit=1
Thread (5 messages)