php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31416 array_walk_recursive() strange behavior
Submitted: 2005-01-05 13:51 UTC Modified: 2005-01-05 23:16 UTC
From: sveta at microbecal dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5CVS-2005-01-05 (dev) OS: Win XP
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: sveta at microbecal dot com
New email:
PHP Version: OS:

 

 [2005-01-05 13:51 UTC] sveta at microbecal dot com
Description:
------------
When I call
array_walk_recursive($array, array('Class', 'static_method1'), 'xxx');
and after it
array_walk_recursive($array, array('Class', 'static_method2'), 'xxx');
static_method2 was not called, but called static_method1

Is it right behavior?

Reproduce code:
---------------
class Test {
    
    public static function rtrim(&$value, $key, $charlist = null) {
        if (is_string($value)) {
            if (null !== $charlist) {
                $value = rtrim($value, $charlist);
            } else {
                $value = rtrim($value);
            }
        }
    }
    
    public static function add_suffix(&$value, $key, $suffix) {
        $value = $value . $suffix;
    }
    
}

$array = array('a' => array('b', 'c'),
               'b' => array('d', 'e'),
               );
array_walk_recursive($array, array('Test', 'add_suffix'), 'xxx');
var_dump($array);
array_walk_recursive($array, array('Test', 'rtrim'), 'xxx');
var_dump($array);

Expected result:
----------------
array(2) {
  ["a"]=>
  array(2) {
    [0]=>
    string(4) "bxxx"
    [1]=>
    string(4) "cxxx"
  }
  ["b"]=>
  array(2) {
    [0]=>
    string(4) "dxxx"
    [1]=>
    string(4) "exxx"
  }
}
array(2) {
  ["a"]=>
  array(2) {
    [0]=>
    string(7) "b"
    [1]=>
    string(7) "c"
  }
  ["b"]=>
  array(2) {
    [0]=>
    string(7) "d"
    [1]=>
    string(7) "e"
  }
}

Actual result:
--------------
array(2) {
  ["a"]=>
  array(2) {
    [0]=>
    string(4) "bxxx"
    [1]=>
    string(4) "cxxx"
  }
  ["b"]=>
  array(2) {
    [0]=>
    string(4) "dxxx"
    [1]=>
    string(4) "exxx"
  }
}
array(2) {
  ["a"]=>
  array(2) {
    [0]=>
    string(7) "bxxxxxx"
    [1]=>
    string(7) "cxxxxxx"
  }
  ["b"]=>
  array(2) {
    [0]=>
    string(7) "dxxxxxx"
    [1]=>
    string(7) "exxxxxx"
  }
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-05 14:05 UTC] tony2001@php.net
Are your sure you tried it with the latest CVS snapshot ?
This issue was fixed about 3 weeks ago and I can't reproduce it with CVS snapshot of any branches under Linux.

 [2005-01-05 14:21 UTC] sveta at microbecal dot com
Yes, I load sources this night and built half hour ago
May be CVS issue? Which file check?
 [2005-01-05 16:02 UTC] tony2001@php.net
Did you remove old php5ts.dll ?
How exactly did you run this code?
What php.exe -v says?
And could you plz try it under some non-MS OS ?
 [2005-01-05 19:42 UTC] sveta at microbecal dot com
I've just re-checkout php-src tree and recompile php. Same result.

>Did you remove old php5ts.dll ?
Yes, I did
>How exactly did you run this code?
pushd path/to/php
php path/to/code.php
>What php.exe -v says?
PHP 5.1.0-dev (cli) (built: Jan  5 2005 21:31:32)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.1.0-dev, Copyright (c) 1998-2004 Zend Technologies
>And could you plz try it under some non-MS OS ?
On FreeBSD and PHP 5.0.3 all work fine (hoster's machine)
 [2005-01-05 21:54 UTC] sveta at microbecal dot com
How to speak in English: "doshlo"?
I've just loaded correct array.c file from PHP 5_0_3 branch, looked into it and a bit changed array.c from my PHP 5_1 branch

Now it works fine. Thank You!

(diff :)
--- array.c.back	2004-11-02 01:29:00.000000000 +0300
+++ array.c	2005-01-05 23:39:52.000000000 +0300
@@ -1157,6 +1157,8 @@
 
 	argc = ZEND_NUM_ARGS();
 	old_walk_func_name = BG(array_walk_func_name);
+	BG(array_walk_fci_cache) = empty_fcall_info_cache;
+	
 	if (argc < 2 || argc > 3 ||
 		zend_get_parameters_ex(argc, &array, &BG(array_walk_func_name), &userdata) == FAILURE) {
 		BG(array_walk_func_name) = old_walk_func_name;
)
 [2005-01-05 22:29 UTC] tony2001@php.net
>How to speak in English: "doshlo"?
"got it", probably.

>I've just loaded correct array.c file from PHP 5_0_3 
>branch, looked into it and a bit changed array.c from my 
>PHP 5_1 branch.
right, that was the fix.
so, you just had an outdated file, right?
next time plz use official CVS snapshots.
 [2005-01-05 23:08 UTC] sveta at microbecal dot com
In cvs.php.net are official CVS snapshots? Or I *must* use precompiled version instead of sources?

I am sorry for this question, but may be next time I'll find another bogus?
 [2005-01-05 23:16 UTC] tony2001@php.net
Official and supported snapshots are at http://snaps.php.net.
It's strongly recommended to try with official ones before posting any report.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 05:01:33 2025 UTC