php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74077 Array_map with null argument inconsistency between one array and multiple arrays
Submitted: 2017-02-10 13:36 UTC Modified: 2017-02-11 21:22 UTC
From: remyfox at hotmail dot com Assigned:
Status: Open Package: Arrays related
PHP Version: 7.1.1 OS: Windows 10
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2017-02-10 13:36 UTC] 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
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-02-11 18:38 UTC] cmb@php.net
-Package: Unknown/Other Function +Package: Arrays related
 [2017-02-11 18:38 UTC] 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-11 21:22 UTC] requinix@php.net
-Summary: Array_map with null argument inconsistency +Summary: Array_map with null argument inconsistency between one array and multiple arrays
 [2017-02-11 21:22 UTC] 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...
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Oct 24 04:01:29 2019 UTC