php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42850 array_walk_recursive() leaves references
Submitted: 2007-10-04 08:10 UTC Modified: 2008-01-16 07:25 UTC
From: ltaupiac at lfdj dot com Assigned:
Status: Closed Package: Arrays related
PHP Version: 5.3.0-dev OS: windows/solaris
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ltaupiac at lfdj dot com
New email:
PHP Version: OS:

 

 [2007-10-04 08:10 UTC] ltaupiac at lfdj dot com
Description:
------------
I have to reopen this bug http://bugs.php.net/bug.php?id=42655 because it was incorrectly closed and tony2001 doesn't seem to receive email.

Here Copy/paste email i send him.

This is not the same bug.

In #34982, an array is modified outside the function func() that call array_walk_recursive. This bug doesn't exist anymore in 5.2.4

The bug i report is when calling array_walk_recursive on an array (callback function doesn't even have reference & for array input and does nothing).
Original array shouldn't be modified, but if you var_dump it, you can see reference on subarray

[0]=> &array(1)

instead of

[0]=>  array(1)

The array shouldn't have been modified but array_walk_recursive leaves references.
This can cause trouble, eg you cant duplicate the original array anymore. Look at following example

$data = array ('key1' => 'val1', array('key2' => 'val2'));

function foo($item, $key) {}; // dumb callback function
var_dump($data);
array_walk_recursive($data, 'foo');
$data2 = $data;      // Duplicate array
$data2[0] = 'altered'; // Alter copy
var_dump($data);
var_dump($data2);

array(2) {
  ["key1"]=> string(4) "val1"
  [0]=> &string(5) "bingo"
}
array(2) {
  ["key1"]=> string(4) "val1"
  [0]=> &string(5) "bingo"
}

both $data and $data2 are altered;


Reproduce code:
---------------
$data = array ('key1' => 'val1', array('key2' => 'val2'));

function dumb($i, $k){}

var_dump($data);
array_walk_recursive($data,'foo');

// Double check the [0]=>&array(1) in actual  result
var_dump($data);

Expected result:
----------------
Expected result:
----------------
array(2) {
  ["key1"]=> string(4) "val1"
  [0]=> array(1) {
    ["key2"]=> string(4) "val2"
  }
}
array(2) {
  ["key1"]=> string(4) "val1"
  [0]=> array(1) {
    ["key2"]=> string(4) "val2"
  }
}

Actual result:
--------------
Actual result:
--------------
array(2) {
  ["key1"]=> string(4) "val1"
  [0]=> array(1) {
    ["key2"]=> string(4) "val2"
  }
}
array(2) {
  ["key1"]=> string(4) "val1"
  [0]=> &array(1) {
    ["key2"]=> string(4) "val2"
  }
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-11-26 08:00 UTC] ltaupiac at lfdj dot com
I have tested on window with apache 2.2.6 the snap.zip you given.

The bug is still there.
A & for reference is still leaved after call array_walk_recursive on val2 and original array can't be duplicate anymore after.

Reproduce code:
--------------
echo PHP_VERSION;
$data = array ('key1' => 'val1', array('key2' => 'val2'));
echo '<pre>';
function foo2($item, $key) {}; // dumb callback function
var_dump($data);
array_walk_recursive($data, 'foo2');
$data2 = $data;      // Duplicate array
$data2[0] = 'altered'; // Alter copy
var_dump($data);
var_dump($data2);

Expected result:
----------------
5.3.0-dev

array(2) {
  ["key1"]=>
  string(4) "val1"
  [0]=>
  array(1) {
    ["key2"]=>
    string(4) "val2"
  }
}
array(2) {
  ["key1"]=>
  string(4) "val1"
  [0]=>
  string(7) "val2"
}
array(2) {
  ["key1"]=>
  string(4) "val1"
  [0]=>
  string(7) "altered"
}

Actual result:
--------------
5.3.0-dev

array(2) {
  ["key1"]=>
  string(4) "val1"
  [0]=>
  array(1) {
    ["key2"]=>
    string(4) "val2"
  }
}
array(2) {
  ["key1"]=>
  string(4) "val1"
  [0]=>
  &string(7) "altered"
}
array(2) {
  ["key1"]=>
  string(4) "val1"
  [0]=>
  &string(7) "altered"
}
 [2008-01-14 22:51 UTC] shire@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2008-01-16 07:25 UTC] ltaupiac at lfdj dot com
I check on win32 snaps built on 16 jan 2008

Everything is correct now.

Thx a lot.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Mar 13 02:01:29 2025 UTC