Bug #74077 [Opn]: Array_map with null argument inconsistency between one array and multiple arrays

From: Date: Sat, 11 Feb 2017 21:22:28 +0000
Subject: Bug #74077 [Opn]: Array_map with null argument inconsistency between one array and multiple arrays
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207274@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74077&edit=1

 ID:                 74077
 Updated by:         requinix@php.net
 Reported by:        remyfox at hotmail dot com
-Summary:            Array_map with null argument inconsistency
+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:

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


Previous Comments:
------------------------------------------------------------------------
[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)

« previous php.bugs (#207274) next »