php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46283 array_merge_recursive() Warning recursion detected in...
Submitted: 2008-10-12 22:14 UTC Modified: 2009-04-30 09:20 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: name at email dot com Assigned:
Status: Closed Package: Arrays related
PHP Version: 5CVS-2008-11-17 OS: *
Private report: No CVE-ID: None
 [2008-10-12 22:14 UTC] name at email dot com
Description:
------------
Calling array_merge_recursive() produces an unclear warning ("recursion detected in"...) and seems to be breaking the script. The above happens if two conditions are met:

1. there is a private/protected data member in the object (see code).
2. the merged arrays contain at least one same string as key.
 
* please note: following code reproduces this problem on the latest snaps of both 5.2.6 and 5.3

Reproduce code:
---------------
class A {
  private $variable;
  public function __construct() {
    $this->variable = 'foo';
  }
}

$x = new A;
$b = array_merge_recursive(array("name"=>$x),array("name"=>$x));
var_dump($b);


Expected result:
----------------
see 'Actual result' below...

Actual result:
--------------
array(1) {
  ["name"]=>
  array(1) {
    ["�A�variable"]=>
    array(2) {
      [0]=>
      string(3) "foo"
      [1]=>
      string(3) "foo"
    }
  }
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-26 19:19 UTC] jani@php.net
What's the problem here? You're passing an object to a function 
expecting arrays and think it will work..?
 [2008-10-29 15:18 UTC] name at email dot com
@jani

1. passing two arrays.
2. they both contain a reference to an object.
3. i expect the function to preserve the structure of the object.
4. notice: ["�A�variable"] <-- whats that?
 [2008-11-17 13:42 UTC] name at email dot com
@jani

PHP Version 5.2.7RC4-dev
Build Date 	Nov 17 2008 11:39:11 

array(1) {
  ["name"]=>
  array(1) {
    ["�A�variable"]=>
    array(2) {
      [0]=>
      string(3) "foo"
      [1]=>
      string(3) "foo"
    }
  }
}
 [2008-11-26 02:41 UTC] lbarnaud@php.net
I can't reproduce with 5.3CVS (no warnings).

The weird output is caused by two null bytes and the class name added to private properties. You get the same output when casting a class to an array.
 [2008-11-28 20:04 UTC] name at email dot com
@lbarnaud

you are correct about the warning.
it might be unrelated to this problem, i thought it was.

about the weird output,
i thought the function supposed to preserve the object, please correct me if im wrong.

thanks
 [2008-12-19 01:39 UTC] jani@php.net
Explain "preserve" ?
 [2008-12-26 00:09 UTC] name at email dot com
@jani

from the doc:

"If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too."

in the example i gave both values are Objects themselves and so i understand that the values of the same string keys should be merged INTO an array but not AS arrays.

did i understand correctly?
 [2009-04-30 09:20 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2009-04-30 09:20 UTC] jani@php.net
Oops, this works fine with current CVS snapshots.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 10:01:32 2024 UTC