php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45126 Can not pass userdata by reference to array_walk_recursive()
Submitted: 2008-05-29 10:55 UTC Modified: 2008-11-26 01:24 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: mark at dynom dot nl Assigned: tony2001 (profile)
Status: Not a bug Package: Arrays related
PHP Version: 5CVS, 6CVS (2008-11-21) OS: *
Private report: No CVE-ID: None
 [2008-05-29 10:55 UTC] mark at dynom dot nl
Description:
------------
Only old-style call pass by reference works with array_merge_recursive, but the 'new' way doesn't

Reproduce code:
---------------
<?php
  $ar     = array('a'=>array(1,5,9,12=>array(1,3,5,200)), 1,3,100, 100=>array(1,9,50));
  
  //--- ONE ------------------------------------
  $result_one = array();
  array_walk_recursive($ar, 'walker_one', $result_one);
  function walker_one($v, $k, &$result)
  {
    $result[ $v ] = $v;
  }
  
  //--- TWO ------------------------------------
  $result_two = array();
  array_walk_recursive($ar, 'walker_two', &$result_two);
  function walker_two($v, $k, $result)
  {
    $result[ $v ] = $v;
  }
  
  var_dump($result_one, $result_two);

Expected result:
----------------
array(7) {
  [1]=>
  int(1)
  [5]=>
  int(5)
  [9]=>
  int(9)
  [3]=>
  int(3)
  [200]=>
  int(200)
  [100]=>
  int(100)
  [50]=>
  int(50)
}
array(7) {
  [1]=>
  int(1)
  [5]=>
  int(5)
  [9]=>
  int(9)
  [3]=>
  int(3)
  [200]=>
  int(200)
  [100]=>
  int(100)
  [50]=>
  int(50)
}

Actual result:
--------------
array(0) {
}
array(7) {
  [1]=>
  int(1)
  [5]=>
  int(5)
  [9]=>
  int(9)
  [3]=>
  int(3)
  [200]=>
  int(200)
  [100]=>
  int(100)
  [50]=>
  int(50)
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-21 17:10 UTC] jani@php.net
Note: I fixed the typo in summary, this is about array_walk_recursive() (and propably array_walk() has same issue)
 [2008-11-21 17:24 UTC] jani@php.net
Seems to be related: bug #39576 (odd that the report talks about array_intersect though?)

Tony, can you check this out?

 [2008-11-21 17:26 UTC] jani@php.net
revision 1.395
date: 2006/11/22 10:58:11;  author: tony2001;  state: Exp;  lines: +3 -3
fix #39576 (array_walk() doesn't separate userdata zval)

 [2008-11-26 01:24 UTC] lbarnaud@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

You need to pass $result explicitly as reference. If you do not, array_walk_recursive uses its own copy of the variable (this still allows it to pass its own copy as reference to the callback function). See other reports about array_walk().
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Aug 06 02:01:24 2020 UTC