php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60662 Additional argument for array_map to ignore references
Submitted: 2012-01-05 03:05 UTC Modified: -
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: phpmpan at mpan dot pl Assigned:
Status: Open Package: Arrays related
PHP Version: 5.3.8 OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phpmpan at mpan dot pl
New email:
PHP Version: OS:

 

 [2012-01-05 03:05 UTC] phpmpan at mpan dot pl
Description:
------------
If a callback function for array_map calls array_map again for array arguments, it is possible to cause infinite recursion for arrays with cyclic references.

While this is NOT a bug and everything works as expected, it would be nice to have a flag to array_map, that address this issue.

Current syntax for array_map prevents adding new parameters at the end (arguments for the callback go there). However an additional optional parameter may be added at the begining. Since the first argument is always a callback, the form with additional flags can be easily distinguished:
array_map(callback, array, ...)                <-- old form
array_map(non-callback, callback, array, ...)  <-- with flags as 1st arg

Test script:
---------------
// WARNING: CAUSES INFINITE RECURSION!

function fn($arg) {
    if (is_array($arg)) {
        return array_map('fn', $arg);
    } else {
        return $arg; // in a real code something will be done here
    }
}

$array = array(1, 2, 3);
$array[] = &$array;

fn($array);


Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC