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
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
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
 [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

Pull Requests

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...
 [2021-06-06 23:17 UTC] 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.
 [2023-04-24 05:06 UTC] vivekkumarshiv7 at gmail dot com
Health Care Magzine are sharing latest news about treatment etc. More info to visit:(https://healthcaremagzine.com)github.com
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 11:01:30 2024 UTC